Re: Negative string offsets
| From: | Sean Coates | Date: | Thu, 17 Apr 2014 15:41:00 +0000 |
| Subject: | Re: Negative string offsets | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-73725@lists.php.net to get a copy of this message | ||
> Yeah, it wouldn't make any sense unless it supported ranges. Something like:
>
> if($filename[-4,4] === '.ext') ...
>
> although, I don't see how that is better than:
>
> if(substr($filename,-4) === '.ext') ...
>
> The latter is much clearer.
FWIW, I realize they're different systems (for one thing, strings are not objects, here), but I
think it's worth considering that this is really useful in Python, and the ranges work a bit
differently (the second value is absolute, not relative, and both values are optional):
>>> "foo.bar.baz"[-4]
'.'
>>> "foo.bar.baz"[-4:]
'.baz'
>>> "foo.bar.baz"[-4:3]
''
>>> "foo.bar.baz"[-4:]
'.baz'
>>> "foo.bar.baz"[-4:-1]
'.ba'
>>> "foo.bar.baz"[-4:11]
'.baz'
>>> "foo.bar.baz"[-4:10]
'.ba'
>>> "foo.bar.baz"[:-4]
'foo.bar'
>>> "foo.bar.baz"[4:7]
'bar'
>>> "foo.bar.baz"[:]
'foo.bar.baz'
Similarly,
: as a range operator (here) makes it clear at a glance that it's not
an array: [4:7] vs [4,7].
S