views:

295

answers:

4

Trying to split this string "主楼怎么走" into separate characters (I need an array) using mb_split with no luck... Any suggestions?

Thank you!

+2  A: 

An ugly way to do it is:

mb_internal_encoding("UTF-8"); // this IS A MUST!! PHP has trouble with multibyte
                               // when no internal encoding is set!
$string = ".....";
$chars = array();
for ($i = 0; $i < mb_strlen($string); $i++ ) {
    $chars[] = mb_substr($string, $i, 1); // only one char to go to the array
}

You should also try your way with mb_split with setting the internal_encoding before it.

bisko
A: 

I would use str_split() for that.

$string = "主楼怎么走";
$array = str_split($string);
echo $array[0]; // would output 主
Johannes Jensen
+1  A: 

try a regular expression with 'u' option, for example

  $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
stereofrog
A: 

Thanks for mb_internal_encoding("utf-8"); function and mb_substr() and mb_split() thanks alot

Wazir khan