Re: Negative string offsets
| From: | Jordi Boggiano | Date: | Mon, 20 Jun 2011 15:24:15 +0000 |
| Subject: | Re: Negative string offsets | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-53411@lists.php.net to get a copy of this message | ||
On 20.06.2011 14:02, Robert Eisele wrote:
> Negative string offsets is a wish and also an implementation of my running
> PHP version for long. It operates in the same fashion like substr() with
> negative offsets, but avoids the function call and is much smarter if one
> single character has to be extracted:
>
> $str = "Hallo";
>
> $str[0] == "H"
> $str[-1] == "o";
>
> If -6 is used as offset, the old warning is displayed because it's the first
> undefined negative offset.
>
> The same thing for setting:
>
> $str[-1] = '0';
> $str[-4] = "4";
>
> will result in "H4ll0"
>
> Would be glad to see this in 5.4
While this in itself is a good thing, I'd prefer to wait some more and
get a well thought-through, full fledged solution supporting ranges i.e.
$str[-1:2] or $str[-1,2].
I believe there were talks of such syntax a few years ago, maybe using
{} instead of []. I mean, right now both [] and {} seem to work equally
on strings and arrays, but changing {} to make it behave more like
substr/array_slice might be a viable BC break (for the negative numbers
that might exist in arrays that is).
Cheers
--
Jordi Boggiano
@seldaek - http://nelm.io/jordi