Home >Backend Development >PHP Tutorial >A simple example of splitting Chinese strings into arrays in PHP

A simple example of splitting Chinese strings into arrays in PHP

WBOY
WBOYOriginal
2016-07-25 08:58:111396browse
This article introduces an example of using PHP to quickly split Chinese strings into arrays for your reference.

In PHP, splitting a string is very simple, mainly using the function preg_match_all. When processing strings containing Chinese characters, you can use the following method:

<?php
    $str = "hi123是中国的";
    preg_match_all("/./u", $str, $arr);
    print_r($arr[0]);
    //by bbs.it-home.org
?>

Output result: Array ( [0] => h [1] => i [2] => fishing [3] => fish [4] => Island [5] => Yes [6] => Medium [7] => Country [8] => )

Note: The mode modifier u is fully supported in php5.

Example 2, split Chinese string with PHP

Split the string "爱伦说" into single words of "爱", "Lotus", and "说".

Use the php function preg_match_all. Example:

<?php
$str = "爱莲说";
preg_match_all('/[\x{4e00}-\x{9fa5}]/u',$str,$string);
dump($string);
//by bbs.it-home.org
?>

Output result: array 0=> array 0 =>string 'love'(length=3) 1 =>string'Lian'(length=3) 2 =>string 'say'(length=3) At this time, to obtain a specific word, you can obtain it through the array.

For information on how to use php to split Chinese and English strings, please refer to the article: Several methods of php splitting Chinese and English strings .



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn