Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51751 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 3236 invoked from network); 30 Mar 2011 14:28:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Mar 2011 14:28:11 -0000 Authentication-Results: pb1.pair.com header.from=landeholm@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=landeholm@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.210.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: landeholm@gmail.com X-Host-Fingerprint: 209.85.210.170 mail-iy0-f170.google.com Received: from [209.85.210.170] ([209.85.210.170:41229] helo=mail-iy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 07/34-07269-8FD339D4 for ; Wed, 30 Mar 2011 09:28:10 -0500 Received: by iyb12 with SMTP id 12so1484092iyb.29 for ; Wed, 30 Mar 2011 07:28:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=7W+NWGnWK6KGy1mzQKg2wccDjE4uTEGmoAfH2TubD2Q=; b=BruEcTjTSg8V9LHGZxyXZUIqcys/TbRXD/GDZ/V0qjbNQQ228Ny1B8c0NV223qb7sW 2WrQ6RSmoKUXFRQ+jif9iwl+FN6t3VbS+5fmce5iOFVMEFayTqHDadGObebZIGKwawYI LcCgOHEKTDNzCaIu2XNgMVyJkZv3oEYR4tXrY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=MY0oieLYfGhg8MggghHFp1zImWQ5qZa6tDlqeYCuqjvrRUP5SBmCOYOq3S4GH4dWKI y6Q0kdkAbN0zqi3FFjlRmdbOzlhmzI908ewkZtvBZ+dQaiXjCzkCD4e1YzBrs7chR64M 8vnSCfbcYBR9JbeIFykWXVmdryow+xna6OpB4= MIME-Version: 1.0 Received: by 10.231.140.81 with SMTP id h17mr1376832ibu.47.1301495285672; Wed, 30 Mar 2011 07:28:05 -0700 (PDT) Received: by 10.231.39.132 with HTTP; Wed, 30 Mar 2011 07:28:05 -0700 (PDT) In-Reply-To: References: <4D92CC38.5040900@toolpark.com> Date: Wed, 30 Mar 2011 16:28:05 +0200 Message-ID: To: Josh Davis , internals@lists.php.net Content-Type: multipart/alternative; boundary=0016e64607222ded19049fb3fce9 Subject: Re: [PHP-DEV] Adding a more logical string slicing function to PHP From: landeholm@gmail.com (Hannes Landeholm) --0016e64607222ded19049fb3fce9 Content-Type: text/plain; charset=ISO-8859-1 "This thread shouldn't be a criticism of substr(), it would be pointless. Its signature and behaviour will never change, unless perhaps around April 1st as a practical joke on the millions of websites it would break." That's a really good joke actually. I can imagine how angry people would get. Maybe PHP could post it as a fake news update tomorrow? And yeah, I wouldn't mind an extra string slicing function although I think substr is sufficient... and if you're adding extra string functions there are more useful ones. These are the string functions in my PHP framework that I use regularly: // Already mentioned: starts_with($subject, $prefix) // returns bool ends_with($subject, $tail) // returns bool // Generating codes/ids: random_str($length = 16) // returns any string random_hex_str($length = 16) // returns any 0-9a-f random_alphanum_str($length = 16, $case_sensitive = true) // returns any a-z0-9/A-Z0-9 from_index($index) // 0 returns a, 1 returns b .. 3 => c.. n => z.. n + 1 => aa.. n + 2 => ab... // like base64 but only uses 0-9a-z (it encodes / as ad, + as ac and a as ab) base64_alphanum_encode($data) base64_alphanum_decode($str) // hex encodes string (eg. "\x11\x1c" => "111c") hex_encode($str) hex_decode($str) email_validate($email) // regex email validation (the actual regex is 390 characters and has never failed me... don't ask me how it works though) http_url_validate($url) in_range($string, $min = -1, $max = -1) // returns true if string has a certain length (easier to read and faster to write) quote($string) // for exporting any string to javascript escaping any escape or control characters automatically (e.g. \n) so "foo\"bar" becomes exactly that ~Hannes On 30 March 2011 16:08, Josh Davis wrote: > On 30 March 2011 15:05, Hannes Landeholm wrote: > > Parsing is a problem in many real-world > > problems and substr currently works great for that purpose. > > That's funny because the first thing I thought when I read the > original mail was "oh that would be great for parsing." In fact, I've > just grep'ed through some code from a rich text parser I've been > working on and at first glance there are at ~5 occurences of substr() > that I would replace with str_slice(). There are also 15+ occurences > of substr() that would remain untouched. It's not black-and-white, > sometimes you want N characters starting from pos X, and other times > you want to cut the text from pos X and pos Y, and that's where > str_slice() would be welcome. > > This thread shouldn't be a criticism of substr(), it would be > pointless. Its signature and behaviour will never change, unless > perhaps around April 1st as a practical joke on the millions of > websites it would break. The question is: is str_slice() useful, does > it fill a need and should it be included into PHP? > > -JD > --0016e64607222ded19049fb3fce9--