PHP support for Chinese string segmentation function _php Tutorial

Source: Internet
Author: User

PHP support for Chinese string segmentation functions


This article to you to share 2 PHP using mb_xxx method to achieve the Chinese character segmentation method, the basic ideas are similar, the need for small partners can refer to.

Str_split does not support Chinese and uses MB_XX functions to implement a

?

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;

}

Method Two:

?

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 mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1007646.html www.bkjia.com true http://www.bkjia.com/PHPjc/1007646.html techarticle PHP Support Chinese string segmentation function This article to share the 2 PHP using the Mb_xxx method to achieve Chinese character segmentation method, the basic ideas are similar, the need for a small partner can ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.