Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:39126 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 979 invoked from network); 21 Jul 2008 12:17:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Jul 2008 12:17:53 -0000 Authentication-Results: pb1.pair.com smtp.mail=sv_forums@fmethod.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=sv_forums@fmethod.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fmethod.com from 69.16.228.148 cause and error) X-PHP-List-Original-Sender: sv_forums@fmethod.com X-Host-Fingerprint: 69.16.228.148 unknown Linux 2.4/2.6 Received: from [69.16.228.148] ([69.16.228.148:55013] helo=host.fmethod.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CF/9A-37768-07E74884 for ; Mon, 21 Jul 2008 08:17:53 -0400 Received: from [83.228.56.37] (port=3297 helo=pc) by host.fmethod.com with esmtpa (Exim 4.69) (envelope-from ) id 1KKuKq-00077t-A0 for internals@lists.php.net; Mon, 21 Jul 2008 07:17:48 -0500 Message-ID: <3025CCBD79414CB2BC9BE1ED3132568C@pc> To: "internals" References: <1216514217.11114.6.camel@localhost> <488295AC.3000107@lerdorf.com> <13F3C3E0FB864C919A2048D507EDB9B9@pc> <48834C31.80806@lerdorf.com> Date: Mon, 21 Jul 2008 15:17:32 +0300 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="UTF-8"; reply-type=response Content-Transfer-Encoding: 8bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5512 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5512 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - host.fmethod.com X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - fmethod.com Subject: Re: [PHP-DEV] New string functions: str_startswith() and str_endswith() From: sv_forums@fmethod.com ("Stan Vassilev | FM") Hi, ucfirst() isn't solving a pain point either. But we use it all the time (I do at least). I'm particularly for begins/endswith() function as I do this all over my code and I'd appreciate the simplification and free extra performance. Regards, Stan Vassilev >I agree that many existing functions can be implemented with a combination >of others, but in this case it is really just one call. The strlen() call >is almost free, and in many cases you wouldn't even use it. If you are >looking for .php files, for example: > > if(str_endswith($path,'.php')) > > vs. > > if(substr($path,-4)=='.php')) > > I just don't see that this is solving any real painpoint. > > -Rasmus > > > Stan Vassilev | FM wrote: >> >> Hi, >> >> Actually starts with and ends with is a very common case. I see your >> concerns, but I can see instantly quite a lot of places in my code where >> I'd use those. And I bet it'll be faster too. >> >> Many of the string functions can be replicated with one-liners using >> other string functions, same for array functions. I still think a very >> common case should be addressed natively. >> >> Regards, >> Stan Vassilev >> >>> 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 >>> >>> >>> -- >>> PHP Internals - PHP Runtime Development Mailing List >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >> >> > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >