Re: PHP, mbstring, UTF-8 and indexing strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux