smarty截取中文乱码问题解决办法
smarty截取中文乱码问题解决办法
本文章提供了三款smarty截取中文乱码问题解决办法,关于乱码主要是在中文汉字中的处理了,我们利用了uft-8,gb2312等字符内码机制来截取字符串。
本文章提供了三款smarty截取中文乱码问题解决办法,关于乱码主要是在中文汉字中的处理了,我们利用了uft-8,gb2312等字符内码机制来截取字符串。
smarty截取中文乱码问题解决办法
本文章提供了三款smarty截取中文乱码问题解决办法,关于乱码主要是在中文汉字中的处理了,我们利用了uft-8,gb2312等字符内码机制来截取字符串。
*/
function smarty_modifier_utruncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false) { if ($length == 0) return ''; if (mb_strlen($string,'utf-8') > $length) { $length -= min($length, mb_strlen($etc,'utf-8')); if (!$break_words && !$middle) { $string = preg_replace('/s+?(s+)?$/', '', mb_substr($string, 0, $length+1,'utf-8')); } if(!$middle) { return mb_substr($string, 0, $length,'utf-8') . $etc; } else { return mb_substr($string, 0, $length/2,'utf-8') . $etc . mb_substr($string, -$length/2,'utf-8'); } } else { return $string; } }
//解决乱码二
function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false) { if ($length == 0) return ''; if (strlen($string) > $length) { $length -= min($length, strlen($etc)); for($i = 0; $i < $length ; $i++) { $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i]; } return $strcut.$etc; } else { return $string; } }
//方法三
function smartdetectutf8($string){ static $result = array(); if(! array_key_exists($key = md5($string), $result)) { $utf8 = " /^(?: [x09x0ax0dx20-x7e] # ascii | [xc2-xdf][x80-xbf] # non-overlong 2-byte | xe0[xa0-xbf][x80-xbf] # excluding overlongs | [xe1-xecxeexef][x80-xbf]{2} # straight 3-byte | xed[x80-x9f][x80-xbf] # excluding surrogates | xf0[x90-xbf][x80-xbf]{2} # planes 1-3 | [xf1-xf3][x80-xbf]{3} # planes 4-15 | xf4[x80-x8f][x80-xbf]{2} # plane 16 )+$/xs "; $result[$key] = preg_match(trim($utf8), $string); } return $result[$key]; } function smartstrlen($string){ $result = 0; $number = smartdetectutf8($string) ? 3 : 2; for($i = 0; $i < strlen($string); $i += $bytes) { $bytes = ord(substr($string, $i, 1)) > 127 ? $number : 1; $result += $bytes > 1 ? 1.0 : 0.5; } return $result; } function smartsubstr($string, $start, $length = null){ $result = ''; $number = smartdetectutf8($string) ? 3 : 2; if($start < 0) { $start = max(smartstrlen($string) + $start, 0); } for($i = 0; $i < strlen($string); $i += $bytes) { if($start <= 0) break; $bytes = ord(substr($string, $i, 1)) > 127 ? $number : 1; $start -= $bytes > 1 ? 1.0 : 0.5; } if(is_null($length)){ $result = substr($string, $i); } else { for($j = $i; $j < strlen($string); $j += $bytes) { if($length <= 0) break; if(($bytes = ord(substr($string, $j, 1)) > 127 ? $number : 1) > 1){ if($length < 1.0) break; $result .= substr($string, $j, $bytes); $length -= 1.0; } else { $result .= substr($string, $j, 1); $length -= 0.5; } } } return $result; } function smarty_modifier_truncate($string, $length = 80, $etc = '...',$break_words = false, $middle = false){ if ($length == 0) return ''; if (smartstrlen($string) > $length) { $length -= smartstrlen($etc); if (!$break_words >> !$middle) { $string = preg_replace('/s+?(s+)?$/', '', smartsubstr($string, 0, $length+1)); } if(!$middle) { return smartsubstr($string, 0, $length).$etc; } else { return smartsubstr($string, 0, $length/2) . $etc . smartsubstr($string, -$length/2); } } else { return $string; } }
本文地址:http://www.phprm.com/frame/34955.html
转载随意,但请附上文章地址:-)