PHP chr () Ord () Chinese intercepting garbled problem solving method _php Skill

Source: Internet
Author: User
Tags chr ord
Copy Code code as follows:

<?php
$lenth = 19;
$str = "How will the long title of the news show only a few words in front of, back with ..." To replace it? ";
echo strlen ($str) <= $lenth? $str: (substr ($str, 0, $lenth). chr (0). " ....");
?>

Copy Code code as follows:
.
/*
@ Another method, using the Ord () function:
@ applies to gb2312 encoding:
/
$str = "How the long title of the news show only the previous words, 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;
}
/*
@ below applies only 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];
}
Return $restr;
}
//Explain the first above: Chr (0) is not null,null is nothing, and Chr (0) is a value of 0. Represented as a 16 binary is 0x00, represented as binary is 00000000 although Chr (0) does not show anything, but he is a character. Although Chr (0) will not show anything, but he is a character. When the Chinese characters are truncated, according to the coding rules he always vlasov the other characters in the back to explain the characters, this is the reason for the garbled.
?>

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.