PHP splits the string into arrays and supports Chinese characters

Source: Internet
Author: User
PHP is split into arrays by string length. Chinese characters are supported? * ** Split the unicode string into an array * @ paramstring $ str input string * @ paraminteger $ l string length * @ returnmixed array or false * functionstr_split_unicode ($ str, $ l PHP is split into arrays by string length and supports Chinese characters

?

/*** Split the unicode string into an array according to the input length * @ param string $ str input string * @ param integer $ l string length * @ return mixed array or false */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 );}

?

 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] => Still ?? [1] =>? Such? [2] => ?? Sss [3] => see gf [4] => g users? [5] => ?? Rtr [6] => et not [7] => bytes) // Array ([0] => s [1] => ss see gf [2] => g do not rt [3] => ret immortal)

?

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.