xml地图|网站地图|网站标签 [设为首页] [加入收藏]
smarty截取字符串truncate函数介绍宝马娱乐在线
分类:网络频道

 代码如下

//方法三

修改smarty内容函数即可了,大家可参考。

smarty截取中文乱码问题解决办法
本文章提供了三款smarty截取中文乱码问题解决办法,关于乱码主要是在中文汉字中的处理了,我们利用了uft-8,gb2312等字符内码机制来截取字符串。
*/

输出结果:

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;
}
}

如果上面是英文肯定没有问题,但是出现中文好像就是乱码了,因为Smarty的truncate截取的是字符(占一个字节),

//解决乱码二

解决办法

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;  
    }  
}

<?php
function smarty_modifier_truncate_utf($string, $length = 80, $etc = '...')
{
 $result = '';
 $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'utf-8');
 for($i = 0, $j = 0; $i < strlen($string); $i++)
 {
  if($j >= $length)
  {
   for($x = 0, $y = 0; $x < strlen($etc); $x++)
   {
    if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
    {
     $x += $number - 1;
     $y++;
    }
    else
    {
     $y += 0.5;
    }
   }
   $length -= $y;
   break;
  }
  if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  {
   $i += $number - 1;
   $j++;
  }
  else
  {
   $j += 0.5;
  }
 }
 for($i = 0; (($i < strlen($string)) && ($length > 0)); $i++)
 {
  if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  {
   if($length < 1.0)
   {
    break;
   }
   $result .= substr($string, $i, $number);
   $length -= 1.0;
   $i += $number - 1;
  }
  else
  {
   $result .= substr($string, $i, 1);
   $length -= 0.5;
  }
 }
 $result = htmlentities($result, ENT_QUOTES, 'utf-8');
 if($i < strlen($string))
 {
  $result .= $etc;
 }
 return $result;
}
?>

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;
}
}

本文由宝马娱乐在线发布于网络频道,转载请注明出处:smarty截取字符串truncate函数介绍宝马娱乐在线

上一篇:宝马娱乐在线网址:分析php://output和php://stdout的 下一篇:没有了
猜你喜欢
热门排行
精彩图文