This article mainly introduces PHP for the Chinese and English mixed string length judgment and interception method, combined with an example of PHP in English string traversal, conversion, interception, calculation and other related operations skills, the need for friends can refer to the next
In this paper, we describe the method of PHP mixed string length judgment and interception in English and Chinese. Share to everyone for your reference, as follows:
/** * * Chinese -English mixed string length judgment * @param unknown_type $str * @param unknown_type $charset */function strlength ($str, $charset = ' Utf-8 ') { if ($charset = = ' Utf-8 ') $str = iconv (' utf-8 ', ' gb2312 ', $str); $num = strlen ($STR); $cnNum = 0; for ($i = 0; $i < $num; $i + +) { if (Ord (substr ($STR, $i + 1, 1)) > 127) { $cnNum + +); $i + +; } } $enNum = $num-($cnNum * 2); $number = ($enNum/2) + $cnNum; return Ceil ($number);}
/** * Chinese-English mixed string interception * @param unknown_type $sourcestr * @param unknown_type $cutlength */function cut_str ($sourcestr, $cutl Ength) {$returnstr = '; $i = 0; $n = 0; $str _length = strlen ($SOURCESTR); The number of bytes in the string while (($n < $cutlength) and ($i <= $str _length)) {$temp _str = substr ($sourcestr, $i, 1); $ascnum = Ord ($temp _str); Get the ASCII code of the $i character in the string if ($ascnum >= 224)//If the ASCII bit is high with 224, {$returnstr = $returnstr. substr ($sourcestr, $i, 3); According to the UTF-8 encoding specification, 3 consecutive characters are counted to a single character $i = $i + 3; The actual byte count is 3 $n + +; String length meter 1} elseif ($ascnum >= 192)//If the ASCII bit is high with 192, {$returnstr = $returnstr. substr ($sourcestr, $i, 2) ; According to the UTF-8 encoding specification, 2 consecutive characters are counted to a single character $i = $i + 2; The actual byte count is 2 $n + +; String length meter 1} elseif ($ascnum >= && $ascnum <= 90)//if uppercase, {$returnstr = $returnstr. substr ($sourcestr, $i, 1); $i = $i + 1; The actual byte count still counts 1 $n + +; But considering overall aesthetics, uppercase letters are counted as a high-bit character} else//In other cases, including lowercase and half-width punctuation, {$returnstr = $returnstr. substr ($sourcestr, $i, 1); $i = $i + 1; The actual byte count is 1 $n = $n + 0.5; Lowercase letters and half-width punctuation with half-high characters justifies ...} } if ($str _length > $cutlength) {$returnstr = $returnstr. "..."; Add an ellipsis at the end of the length to return $returnstr;}