Re: Re: foreach() for strings
| From: | Tomas Kuliavas | Date: | Mon, 20 Jun 2011 21:18:51 +0000 |
| Subject: | Re: Re: foreach() for strings | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-53450@lists.php.net to get a copy of this message | ||
2011.06.20 21:38 Robert Eisele rašė:
> I really like the ideas shared here. It's a thing of consideration that
> array-functions should also work with strings. Maybe this would be the way
> to go, but I'm more excited about the OOP implementation of TextIterator
> and
> ByteIterator, which solves the whole problem at once (and is easier to
> implement, as mentioned by Stas). As Jonathan said, Database results with
> a
> certain encoding could get iterated, too. The only way to workaround the
> Text/Byte problem would be, offsetting >EVERY< string with 1-2 byte
> "string-type" information or an additional type flag in the
> zval-strcuture.
> Handling everything with zval's instead of objects would have the
> advantage,
> that database-layers like mysqlnd could write the database-encoding
> directly
> into the zval and the user had no need to decide what encoding is used.
>
> A new casting operator (binary) could then cast the string to a 1-byte
> array. But this is syntactical sugar over OOP-implementations - I don't
> know
> which one is the better choice.
>
> For example:
>
> $utf8_string = "Jägermeister"; // information of utf8 ist stored in the
> zval
>
> foreach ($utf8_string as $k => $v) // would iterate in byte mode
>
> foreach ((binary)$utf8_string as $k => $v) // would iterate in text mode
>
> over this:
> $utf8_obj = new ByteIterator("Jägermeister");
>
> foreach ($utf8_obj as $k => $v)
>
> foreach ($utf8_obj->toText() as $k => $v)
>
>
> I think the first one is easier and would be nicer to average developers
> (and lazy programmers like me ;o) )
You assume that string is in utf-8. It can be some iso-8859-x,
iso-2022-xx, utf-7, utf-16 or any other multibyte character set.
If you want to parse string in symbols, use mb_substr and mb_strlen, set
charset correctly and make sure that your string is in correct character
set, if PHP bug about inconsistent symbol position calculation is still
unfixed.
--
Tomas