Php: How to segment Chinese characters with specified characters and put each segment into array elements

Source: Internet
Author: User
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

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.