Re: PHP, mbstring, UTF-8 and indexing strings
| From: | Jochem Maas | Date: | Sun, 24 Jun 2007 22:37:07 +0000 |
| Subject: | Re: PHP, mbstring, UTF-8 and indexing strings | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-257737@lists.php.net to get a copy of this message | ||
delsvr wrote:
> Just a simple question (with a simple answer, hopefully): how can I access
> character x of a utf-8 encoded string? i.e. how would I get this to work:
>
> for($x=0; $x < strlen($utf8); $x++)
> echo "character {$x}:" . $utf8{$x};
>
> Keep in mind that I have strlen overloaded by mbstring, and this is
> necessary for my purposes because I need to do work on each character. Also,
> by "character" I mean, for example: "我".
substr() should do it.
I don't think you can loop through a multibyte string in the way you want.
$l = strlen($utf8);
for($x=0; $x < $l; $x++) echo "char {$x}:".substr($utf8, $x, 1);
check here to figure out exactly which functions you
are currently overloading:
http://php.net/mb_string
>
> Thanks,
> delsvr