PHP chr () Ord () Chinese truncation garbled problem solving method _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
$lenth = 19;
$str = "How will the long title of the news show only a few words in front, followed by ..." To replace it? ";
echo strlen ($str) <= $lenth? $str: (substr ($str, 0, $lenth). chr (0). " ....");
?>

Copy CodeThe code is as follows:
/*
@ Another method, use the Ord () function:
@ Applies to gb2312 encoding:
*/
$str = "How will the long title of the news show only a few words in front, followed by ..." To replace it? ";
function Gb2312_substr ($STR, $limit) {
$restr = ";
for ($i =0; $i < $limit-3; $i + +) {
$restr. = Ord ($str [$i]) >127? $str [$i]. $str [+ + $i]: $str [$i];
}
return $restr;
}
/*
@ The following only applies to utf-8 encoding;
*/
function Utf8_substr ($STR, $limit) {
$restr = ";
for ($i =0; $i < $limit-3; $i + +) {
$restr. = Ord ($str [$i]) >127? $str [$i]. $str [+ + $i]. $str [+ + $i]: $str [$i];
}
return $restr;
}
Explain the first of the above: Chr (0) is not null,null is nothing, and Chr (0) is the value of 0. Expressed as 16 binary is 0x00, denoted as binary is 00000000 although Chr (0) will not show anything, but he is a character. Although Chr (0) will not show anything, but he is a character. When the Chinese character is truncated, according to the coding rules he always vlasov the other words in the back together as a Chinese character interpretation, which is the reason for garbled.
?>

http://www.bkjia.com/PHPjc/319409.html www.bkjia.com true http://www.bkjia.com/PHPjc/319409.html techarticle Copy the code as follows: PHP $lenth = n; $str = "How to put a long title of the news to show only the preceding words, followed by ..." To replace it? "; echo strlen ($STR) = $lenth? $STR: (Sub ...

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