100 split string length

Source: Internet
Author: User
100 evaluate the length of the detection string $ str [0] = & quot; 123asd Hello [haha] & quot; $ str [1] = & quot; 123asd Hello [] & quot; // A Chinese character (UTF-8 encoded) symbol [here the word and the symbol outside are counted as one character] & nbsp; then, the calculated length is $ str [0] and $ str [1], which is 100 points.
$ Str [0] = "123asd Hello [haha]"
$ Str [1] = "123asd Hello [Hi!]"
// One Chinese character and one character (UTF-8 encoding) symbol [here the word and the symbol outside are counted as one character]
Then calculate the length
The result is $ str [0] and $ str [1] with a length of 9.

It is best to write a function. Thank you.


------ Solution --------------------
I didn't quite understand the subject, but there was a function at hand.
PHP code
/*** Get the utf8 length * @ author mu_rain * @ param String $ str * @ return Int */static function strlen_utf8 ($ str) {$ I = 0; $ count = 0; $ len = strlen ($ str); while ($ I <$ len) {$ chr = ord ($ str [$ I]); $ count ++; $ I ++; if ($ I >=$ len) break; if ($ chr & 0x80) {$ chr <= 1; while ($ chr & 0x80) {$ I ++; $ chr <= 1 ;}} return $ count ;}
------ Solution --------------------
PHP code
Function myLen ($ str, $ startTag = '[', $ endTag = ']', $ encoding = 'utf-8') {$ st = preg_quote ($ startTag ); $ et = preg_quote ($ endTag); return mb_strlen (preg_replace ("# {$ st} [^ {$ et}] * {$ et }#","~ ", $ Str), $ encoding);} echo myLen (" 123asd Hello [haha] "); // 9
------ Solution --------------------
A bunch of string functions are provided, which is slow to use.
PHP code
      

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.