php支持中文字符串分割的函数_PHP教程

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

php支持中文字符串分割的函数

  本文给大家分享了2个php使用mb_xxx方法来实现中文字符分割的方法,其基本思路都差不多,有需要的小伙伴可以参考下。

  str_split不支持中文,利用mb_xx函数实现个

  ?

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

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

$arr = array();

for($i=0;$i

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

$arr[] = $s;

}

return $arr;

}

  方法二:

  ?

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;

}

  以上所述就是本文的全部内容了,希望大家能够喜欢。

www.bkjia.com true http://www.bkjia.com/PHPjc/1007646.html TechArticle php支持中文字符串分割的函数 本文给大家分享了2个php使用mb_xxx方法来实现中文字符分割的方法,其基本思路都差不多,有需要的小伙伴可...
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!