Home > Backend Development > PHP Tutorial > PHP supports Chinese string splitting function_PHP tutorial

PHP supports Chinese string splitting function_PHP tutorial

WBOY
Release: 2016-07-13 09:52:43
Original
1172 people have browsed it

PHP functions that support Chinese character string segmentation

This article shares with you two PHP methods that use the mb_xxx method to achieve Chinese character segmentation. The basic ideas are similar. If necessary Friends can refer to it.

str_split does not support Chinese, use the mb_xx function to implement

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

/**

* Convert a string to an array

* @param string $str

* @param number $split_length

* @return multitype:string

*/

function mb_str_split($str,$split_length=1,$charset="UTF-8"){

if(func_num_args()==1){

return preg_split('/(?

}

if($split_length<1)return false;

$len = mb_strlen($str, $charset);

$arr = array();

for($i=0;$i<$len;$i =$split_length){

$s = mb_substr($str, $i, $split_length, $charset);

$arr[] = $s;

}

return $arr;

}

1

2

3

1

2

3

4

5

6

7

8

9

10

function mbStrSplit ($string, $len=1) {

$start = 0;

$strlen = mb_strlen($string);

while ($strlen) {

$array[] = mb_substr($string,$start,$len,"utf8");

$string = mb_substr($string, $len, $strlen,"utf8");

$strlen = mb_strlen($string);

}

return $array;

}

4

5

6

8 9 10 11 12
13 14
15 16 17 18 19
/** * Convert a string to an array * @param string $str * @param number $split_length * @return multitype:string */ function mb_str_split($str,$split_length=1,$charset="UTF-8"){ if(func_num_args()==1){ return preg_split('/(?
Method 2:  ?
1 2 3 4 5 6 7 8 9 10 function mbStrSplit ($string, $len=1) { $start = 0; $strlen = mb_strlen($string); while ($strlen) { $array[] = mb_substr($string,$start,$len,"utf8"); $string = mb_substr($string, $len, $strlen,"utf8"); $strlen = mb_strlen($string); } return $array; }
The above is the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/1007646.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1007646.htmlTechArticlephp functions that support Chinese string segmentation This article shares with you 2 PHP methods that use the mb_xxx method to achieve Chinese character segmentation The basic ideas of the methods are similar. Friends in need can...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template