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;
}
}
我的博物馆故事 官方安卓版v1.61.2
我的博物馆故事是一款以消除为主题的经营养成类手游,在这里玩家
专业模拟飞行10 手机版v12.2.4
专业模拟飞行10安卓版是一款飞行休闲手游,顶尖的物理飞行引擎
动物起义战斗模拟器二琳同款 最新版v4.1.1
动物起义战斗模拟器是一个非常有趣的模拟类游戏,玩家可以召唤各
迷你世界七周年 安卓手机版v1.43.0
迷你世界7周年是一款由《迷你世界》官方推出的庆祝特别版本,在
劫后公司无限资源版 v1.0.5.1
劫后公司内置菜单版是游戏的破解版本,在该版本中为玩家提供了内