Hi Lester, On Fri, Feb 27, 2015 at 9:53 PM, Lester Caine <les...@lsces.co.uk> wrote:
> > Please, read the examples again, the current behavior is nothing but > > inconsistent: > > > > substr("a", 1) => FALSE > > substr("a", -300) => "" > > ? That was the case prior to PHP5.2.1 > The fixes in 5.2.2 were not commonly accepted but give false for both, > but 5.2.7 and later give false and "a" which is what was the preferred > result at the time. Unless we are seeing something different, I'm only > seeing "a" or false for a current output of all your examples. > I meant "a", but you are right, it's a bit less inconsistent than I thought. The current behavior could be defined as "if the resulting slice is so that (start index <= end index) and (either start index or end index is in bound) return the slice, else return FALSE", which is not very useful but not so bad anyway. The only real annoyance is that the check is strict on the right bound, so that: substr("abcd", 5) => FALSE while: substr("abcd", -10, -4) => "" That explains one of the E_DEPRECATED triggered by Drupal 7, because when you want to remove a prefix from a string, you often do: if (substr($str, 0, strlen($prefix)) { $str = substr($str, strlen($prefix)) } But this currently returns FALSE when $str == $prefix. (Obviously, the most useful behavior would be to return a string in all cases, like for example, Python.) Damien