Home >Backend Development >PHP Problem >PHP Chinese and English string to array
In PHP, sometimes we need to convert a string into an array. When a string contains English words or Chinese characters, we may need to split it into array elements. Here, this article will introduce in detail how to convert Chinese and English strings into arrays.
First, let’s take a look at how to break up a string into array elements in PHP. This can be achieved using the explode() function in PHP:
$str = "Hello World"; $arr = explode(" ", $str); print_r($arr);
The output result of this program is:
Array ( [0] => Hello [1] => World )
In the above program, spaces are used as separators to break up the string into array elements. . Among them, the first parameter of the explode() function specifies the separator, and the second parameter is the string variable to be broken up.
Next, we consider how to convert a mixed Chinese and English string into an array. Here we need to use PHP regular expressions to split strings. Taking a mixed sentence of Chinese and English as an example, we can use the following program to convert a string to an array:
$str = "Hello,世界"; preg_match_all("/./u", $str, $matches); print_r($matches[0]);
The output of the above program is as follows:
Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => , [6] => 世 [7] => 界 )
Here, we use The preg_match_all() function splits a string into an array based on a regular expression. Among them, the regular expression "/./u" means matching all characters (including Chinese), and u means parsing in UTF-8 encoding, ensuring that Chinese characters can be processed correctly.
The above program uses the preg_match_all() function to break the string into array elements. If there are Chinese characters in the string, it will be divided into characters normally. From the output results, we can see that the program stores all the scattered characters into the array variable $matches[0].
If we want to store the English and Chinese characters in the string separately into different array elements, we can use the following program:
$str = "Hello,世界"; preg_match_all("/[x{4e00}-x{9fa5}]+|[A-Za-z]+/u", $str, $matches); print_r($matches[0]);
The output of this program is as follows:
Array ( [0] => Hello [1] => 世界 )
In the above program, the regular expression "/[x{4e00}-x{9fa5}] |[A-Za-z] /u"
is used, where [x{4e00} -x{9fa5}] means matching Chinese characters, [A-Za-z] means matching English characters.
If we need to convert Chinese characters into Pinyin, and the Pinyin is also stored as an array element, we can use the following program:
$str = "你好,世界"; preg_match_all("/[x{4e00}-x{9fa5}]+|[A-Za-z]+/u", $str, $matches); for($i=0; $i<count($matches[0]); $i++){ $str = $matches[0][$i]; if (preg_match("/[x{4e00}-x{9fa5}]/u", $str)) { $pinyin = PinYin($str); $arr[] = $pinyin[0]; } else { $arr[] = $str; } } print_r($arr);
The running result of this program is as follows:
Array ( [0] => ni [1] => hao [2] => shi [3] => jie )
In the above program, we encapsulate a function PinYin(), which is used to convert Chinese characters into Pinyin. In the program, as long as the string contains Chinese characters, this function will be called to convert the Chinese characters into Pinyin and store them in the array variable $arr.
At this point, we have successfully converted Chinese and English strings into arrays. If you need to perform character processing and text analysis, converting a string into an array will become one of your commonly used PHP functions.
The above is the detailed content of PHP Chinese and English string to array. For more information, please follow other related articles on the PHP Chinese website!