100 minutes to find the length of a check and split string

Source: Internet
Author: User
100 to find the length of a test string
$str [0] = "123ASD hello [haha]"
$STR [1] = "123ASD Hello [Hello, not good]"
A Chinese character (utf-8 encoding) symbol [the word inside and the outer symbol is counted as a character]
Then calculate the length
The result is $str[0] and $str[1] for length 9

It's best to write a function. Thank you.


------Solution--------------------
I don't quite understand the landlord's question, but the function, there is one on hand.
PHP Code
/** * Get UTF8 length * @author mu_rain * @param String $str * @return Int */static function Strlen_utf        8 ($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 attached and are used slowly.
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.