Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:39107 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 30252 invoked from network); 20 Jul 2008 01:32:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jul 2008 01:32:38 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@lerdorf.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@lerdorf.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lerdorf.com from 204.11.219.139 cause and error) X-PHP-List-Original-Sender: rasmus@lerdorf.com X-Host-Fingerprint: 204.11.219.139 mail.lerdorf.com Received: from [204.11.219.139] ([204.11.219.139:44841] helo=mail.lerdorf.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 49/CB-13864-5B592884 for ; Sat, 19 Jul 2008 21:32:37 -0400 Received: from [192.168.200.148] (c-24-6-219-206.hsd1.ca.comcast.net [24.6.219.206]) (authenticated bits=0) by mail.lerdorf.com (8.14.3/8.14.3/Debian-5) with ESMTP id m6K1WSp5029629; Sat, 19 Jul 2008 18:32:28 -0700 Message-ID: <488295AC.3000107@lerdorf.com> Date: Sat, 19 Jul 2008 18:32:28 -0700 User-Agent: Thunderbird/3.0a2pre (Macintosh; 2008071516) MIME-Version: 1.0 To: Lars Strojny CC: Martin Jansen , internals References: <1216514217.11114.6.camel@localhost> In-Reply-To: <1216514217.11114.6.camel@localhost> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-3.0 (mail.lerdorf.com [204.11.219.139]); Sat, 19 Jul 2008 18:32:29 -0700 (PDT) Subject: Re: [PHP-DEV] New string functions: str_startswith() and str_endswith() From: rasmus@lerdorf.com (Rasmus Lerdorf) For the start of the string: substr($haystack,0,strlen($needle)) == $needle And for the end of the string: substr($haystack,-strlen($needle)) == $needle For case-insensitivity, just strtolower both. Writing built-in functions for something that can be done with trivial one-liners isn't something we typically do. -Rasmus Lars Strojny wrote: > Hi Martin, > > first of all, thanks for you work! A few comments below. > > Am Samstag, den 19.07.2008, 14:55 +0200 schrieb Martin Jansen: >> Attached you'll find a patch against PHP_5_3 that implements two new >> string functions: >> >> str_startswith(haystack, needle [, case_sensitivity]) > > That's in my opinion too easy to solve with strpos($haystack, $needle) > === 0. > >> checks if haystack starts with needle. The check is performed >> case-insensitively, but this can be overridden by passing TRUE as the >> value for the third parameter. The second function >> >> str_endswith(haystack, needle [, case_sensitivity]) > [...] > > Suffix checking is indeed a bit harder. But I'm not sure if we should > introduce a function for every special case. Suffix verification often > is about verifying file extensions. That could be easily solved with > pathinfo() (and to make that easier, it would be more helpful to allow > the engine to directly access returned arrays like > pathinfo($file)['extension']). The other thing I would love to see is > something I really love in Ruby: you have various ways to work with > substrings[1]. For example you can do "foobar"[-3,3] == "bar" to check > the suffix. That's in my opinion much easier and flexible. > > So -1 from me for the new string functions, but +1 for niftier variants > to work with offsets. > > [1] http://pleac.sourceforge.net/pleac_ruby/strings.html > > cu, Lars