This article mainly introduces PHPfor mixed Chinese and English stringsLength judgment and interception methods, combined with examples to analyze the traversal of Chinese and English strings in php, conversion, interception, calculation and other related operation skills, friends in need can refer to the following
This article describes the PHP method for length judgment and interception of mixed Chinese and English strings. Share it with everyone for your reference, the details are as follows:
/** * * 中英混合字符串长度判断 * @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 ); }
The above is the detailed content of PHP detailed examples of length judgment and interception methods for Chinese and English mixed strings. For more information, please follow other related articles on the PHP Chinese website!