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 the Code code 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.
?>
The above describes the problem to solve the PHP CHR Ord Chinese interception garbled problem resolution, including the issue of the content, I hope that the PHP tutorial interested in a friend helpful.