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