Copy The code is as follows: /* @ Another method, use the ord() function: @ Applicable"/> Copy The code is as follows: /* @ Another method, use the ord() function: @ Applicable">
Home >Backend Development >PHP Tutorial >Problem Solving PHP chr ord Chinese interception garbled problem solution
Copy the code The code is as follows:
$lenth = 19;
$str = "How to display only the first few words of a long news title, and then use... Replace? ";
echo strlen($str)<=$lenth ? $str : (substr($str,0,$lenth).chr(0)."....");
?>
Copy the code The code is as follows:
/*
@ Another method, use the ord() function:
@ Applicable to gb2312 encoding:
*/
$str = "How Display only the first few words of a long news title and replace it with...? ";
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 one above: chr(0) is not null, null means nothing, And the value of chr(0) is 0. Expressed as hexadecimal, it is 0x00, and expressed as binary, it is 00000000. Although chr(0) will not display anything, it is a character. Although chr(0) does not display anything, it is a character. When a Chinese character is truncated, according to the encoding rules, it always has to pull in other characters behind it and interpret them as Chinese characters. This is the reason why garbled characters appear.
?>
The above introduces the problem solving method of php chr ord Chinese interception garbled problem, including the content of problem solving. I hope it will be helpful to friends who are interested in PHP tutorials.