截图多余字符,中英文都可以,避免截取中文结尾 ?? 乱码!!
- function utf8_strlen($string = null) {
- // 将字符串分解为单元
- preg_match_all('/./us', $string, $match);
- // 返回单元个数
- 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);
-
-
-
- /**
- * 字符串截取,支持中文和其他编码
- * @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;
- }
- }
复制代码
|