"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 <php...@gmail.com> wrote: > On 30 March 2011 15:05, Hannes Landeholm <landeh...@gmail.com> 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 >