Chinese-supported PHP splits the array code by string length, _php tutorial

Source: Internet
Author: User

Chinese-supported PHP splits the array code by string length,


Here is the code I wrote to enable the segmentation of Chinese and English mixed characters:

<?phpfunction 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;} Header (' Content-type:text/html;charset=utf-8 '); $str = ' I love Beijing 3 I love Shanghai-I love Xianggang '; $r = Mbstrsplit ($str, 4); Echo '
';p rint_r ($r); Echo '
';? >

Operation Result:

Array (  [0] = = I love beijing  [1] = 3 I love  [2] = + Sea-I love  [3] = Xian  [4] = Ggan  [5] = g)
<?php function Str_split_unicode ($str, $l = 0) {if ($l > 0) {$ret = Array (), $len = Mb_strlen ($str, "UTF-8"); for ( $i = 0; $i < $len; $i + = $l) {$ret [] = Mb_substr ($str, $i, $l, "UTF-8");} return $ret;} Return Preg_split ("//u", $str,-1, preg_split_no_empty);}? > $s = "not rtret, SSS see GFG not Immortal"; Mild Milk Print_r (Str_split ($s, 5)); Print_r (Str_split_unicode ($s, 5));//Output//array ([0] = [1] = = [2] = SSS [3] = See GF [4] = g [ 5] + RTR [6] = et not [7] = decay)//array ([0] = = Not yet desired s [1] = SS See GF [2] = g no RT [3] = ret Immortal)

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1000258.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000258.html techarticle Support Chinese PHP by string length to split the number of groups of code, the following is the code I wrote, to achieve the split between Chinese and English mixed characters: phpfunction mbstrsplit ($string, $len =1) {...

  • Related Article

    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.