Php: How to segment Chinese characters in the specified length and put each segment into an array element, such as $ cont & quot; about cooking. in fact, this set of magic novels for chefs has been brewing for a long time, but I have always felt that they are not mature, so I have not written any more. airspeed ...... & Quot; functionsubpart ($ cont, 10, & quot; utf8 & quot ;){......} $ Arraysubpart ($ cont, 1 php: How to segment Chinese characters with the specified length and add each segment to an array element
For example:
$ Cont = "about cooking, this set of magic novels for chefs has been brewing for a long time, but I have always felt immature, so there is no such thing as airspeed ...... ";
Function subpart ($ cont, 10, "utf8 "){......}
$ Array = subpart ($ cont, 10, "utf8 ");
$ Array = ([0] => "about cooking. ", [1] =>" This set of chefs ", [2] =>" magic novels ",......);
How should I write the subpart () function?
------ Solution --------------------
Function subpart ($ cont, $ l, $ utf)
{
$ Len = mb_strlen ($ cont, $ utf );
For ($ I; $ I <$ len; $ I + = $ l)
$ Arr [] = mb_substr ($ cont, $ I, $ l, $ utf );
Return $ arr;
}
$ Cont = 'people of China, People's Bank of China ';
Print_r (subpart ($ cont, 5, "utf8 "));
------ Solution --------------------
$ Cont = "about cooking, this set of magic novels for chefs has been brewing for a long time, but I have always felt immature, so there is no such thing as airspeed ...... ";
$ Array = subpart ($ cont, 10, "utf8 ");
Echo'
';
Print_r ($ array );
Function subpart ($ cont, $ len, $ charset)
{
$ Contlen = strlen ($ cont );
For ($ I = 0; $ I <= $ contlen; $ I + = $ len)
{
$ Contarr [] = substr ($ cont, $ I, $ len );
}
Return $ contarr;
}
?>
------ Solution --------------------
The upstairs methods are correct. of course, you can also write regular expressions to match the characters to be separated, such as ', ''| '.
Then use substr to load the array