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