Screenshots of extra characters can be made in both Chinese and English. Can you avoid capturing the Chinese ending? ? Garbled code! !
- function utf8_strlen($string = null) {
- // Decompose the string into units
- preg_match_all('/./us', $string, $match);
- // Return the number of units
- return count( $match[0]);
- }
- function sub_content($content, $length){
- $len = utf8_strlen($content);
- for($i = 0 ; $i < $len ; $i++){
- $arr[$i] = mb_substr($content,$i,1,'utf-8');
- }
- $get_length = 0;
- $result = '';
- foreach($arr as $code){
- $result .= $code;
-
- if(strlen($code) > 1){
- $get_length += 2;
- }else{
- $get_length += 1;
- }
- if($get_length >= $ length){
- break;
- }
- }
- return $result;
- }
- echo sub_content($rows["Description"],18);
-
-
-
- /**
- * String interception, supports Chinese and other encodings
- * @param string $str
- * @param int $start
- * @param int $length
- * @param string $charset
- * @param boolean $suffix
- * @return string
- */
- function w_substr($str , $start = 0, $length, $charset = "utf-8", $suffix = TRUE) {
- $suffix_str = $suffix ? '…' : '';
- if(function_exists('mb_substr')) {
- return mb_substr($str, $start, $length, $charset) . $suffix_str;
- } elseif(function_exists('iconv_substr')) {
- return iconv_substr($str, $start, $length, $charset) . $suffix_str ;
- } else {
- $pattern = array();
- $pattern['utf-8'] = '/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80 -xbf]{2}|[xf0-xff][x80-xbf]{3}/';
- $pattern['gb2312'] = '/[x01-x7f]|[xb0-xf7][xa0-xfe] /';
- $pattern['gbk'] = '/[x01-x7f]|[x81-xfe][x40-xfe]/';
- $pattern['big5'] = '/[x01-x7f]| [x81-xfe]([x40-x7e]|xa1-xfe])/';
- preg_match_all($pattern[$charset], $str, $matches);
- $slice = implode("", array_slice($matches [0], $start, $length));
- return $slice . $suffix_str;
- }
- }
Copy code
|