Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Split multibyte string (word) into separate characters

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

Thank you!

like image 968
Peterim Avatar asked Mar 31 '10 20:03

Peterim


People also ask

How to divide a string into various elements in PHP?

explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.

How can I split sentences into words in PHP?

To split a string into words in PHP, use explode() function with space as delimiter. The explode() function returns an array containing words as elements of the array.

What is multibyte string PHP?

Mbstring stands for multi-byte string functions. Mbstring is an extension of php used to manage non-ASCII strings. Mbstring is used to convert strings to different encodings. Multibyte character encoding schemes are used to express more than 256 characters in the regular byte wise coding system.

How do I get the first character of a string in PHP?

To get the first character from a string, we can use the substr() function by passing 0,1 as second and third arguments in PHP.


2 Answers

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

  $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
like image 198
user187291 Avatar answered Sep 22 '22 08:09

user187291


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.

like image 22
bisko Avatar answered Sep 18 '22 08:09

bisko