PHP UTF8 Chinese character truncation function code _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/* UTF-8 Chinese character truncation program */
$STR = "123 This is the test string";
$str 1 = "() ()";
Echo subutf8str ($str, 0, 3). "
";
Echo subutf8str ($str, 0,4). "
";
Echo subutf8str ($str 1,0,4). "
";
Echo subutf8str ($str 1,0,10). "
";
function Subutf8str ($str, $start =0, $length =80) {
$cur _len = 0; People understand the length of the string
$all _len = strlen ($STR); Machine Understanding String Length
if ($length > $all _len)
{
return $str;
}
for ($i = 0; $i < $all _len;)
{
if ($cur _len = = $start)
{
Break
}
if (Ord ($str [$i]) > 127)
{
$i + = 3;
}else{
$i + = 1;
}
$cur _len + +;
}
$start _pos = $i;
$temp _pos = $cur _len;
for (; $cur _len-$temp _pos < $length;)
{
if ($i >= $all _len)
Break
if (Ord ($str [$i]) > 127)
{
$i + = 3;
}else{
$i + = 1;
}
$cur _len + +;
}
$end _pos = $i;
Return substr ($str, $start _pos, $end _pos);
}
?>

In fact, PHP is originally born with many CharSet under the character interception scheme, the amount, so this is the way ... It's embarrassing.
Multibyte String functions function family,

String Mb_substr (string $str, int $start [, int $length [, String $encoding]]) is used to intercept strings
int Mb_strlen (String $str [, String $encoding]) returns the string length
....
Please see the PHP manual for details.

http://www.bkjia.com/PHPjc/326024.html www.bkjia.com true http://www.bkjia.com/PHPjc/326024.html techarticle Copy the code as follows:? PHP/* UTF-8 Chinese character Truncation program */$str = "123 This is the test string"; $str 1 = "() ()"; Echo subutf8str ($str, 0, 3). " BR "; Echo subutf8str ($str, 0,4) ...

  • 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.