This article describes the PHP's judgment and intercepting method for mixed Chinese and English strings. Share it 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, $cutlength) { $returnstr = ''; $i = 0; $n = 0; $str_length = strlen ( $sourcestr ); //The number of bytes of 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 ASCII bit is high and 224, { $returnstr = $returnstr . substr ( $sourcestr, $i, 3 ); // According to UTF-8 encoding specification, 3 consecutive characters are counted as single characters $i = $i + 3; //The actual Byte is counted as 3 $n ++; //String length meter 1 } elseif ($ascnum >= 192) //If ASCII bit is high and 192, { $returnstr = $returnstr . substr ( $sourcestr, $i, 2 ); // According to UTF-8 encoding specification, 2 consecutive characters are counted as single characters $i = $i + 2; //The actual Byte is counted as 2 $n ++; //String length meter 1 } elseif ($ascnum >= 65 && $ascnum <= 90) //If it is capital letters, { $returnstr = $returnstr . substr ( $sourcestr, $i, 1 ); $i = $i + 1; //The actual number of Bytes is still counted 1 $n ++; //But considering the overall beauty, capital letters are counted as high-ranking characters } else //In other cases, including lowercase letters and half-width punctuation marks, { $returnstr = $returnstr . substr ( $sourcestr, $i, 1 ); $i = $i + 1; //The actual number of Bytes is counted 1 $n = $n + 0.5; //Lowercase letters and half-width punctuation are wide with half-high characters... } } if ($str_length > $cutlength) { $returnstr = $returnstr . "..."; //Add ellipsis at the end when the length exceeds the } return $returnstr; }
PS: Here are a few online character statistics tools for your reference:
Online word count tool:
http://tools./code/zishutongji
Online character statistics and editing tools:
http://tools./code/char_tongji
For more information about PHP related content, please check out the topic of this site:Summary of usage of php strings》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《PHP object-oriented programming tutorial》、《Summary of PHP network programming skills》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.