PHP that supports Chinese characters is split into array code by string length

Source: Internet
Author: User
PHP compiled by myself is divided into array code by string length. it supports Chinese characters. the following code and usage are provided. if you need it, refer to it. The following code is used to separate Chinese and English 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'
';print_r($r);echo '
';?>

Running 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 = "if you do not want sss to see gfg do not rtret immortal"; // Mild milk print_r (str_split ($ s, 5); print_r (str_split_unicode ($ s, 5 )); // output // Array ([0] => [1] => �� for example, [2] => �� sss [3] => see gf [4] => g users [5] => �� rtr [6] => et does not [7] => bytes) // Array ([0] => s [1] => ss see gf [2] => g do not rt [3] => ret immortal)

The above is all the content of this article. I hope you will like it.

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.