欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

smarty中改進(jìn)truncate使其支持中文的方法

 更新時間:2016年05月30日 09:19:52   作者:keyunq  
這篇文章主要介紹了smarty中改進(jìn)truncate使其支持中文的方法,涉及針對Smarty源碼中truncate源文件進(jìn)行函數(shù)功能擴展的相關(guān)技巧,需要的朋友可以參考下

本文實例講述了smarty中改進(jìn)truncate使其支持中文的方法。分享給大家供大家參考,具體如下:

smarty的truncate不支持中文的截取。將smarty目錄下plugins中的modifier.truncate.php改成下面這個樣子就可以了

<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type:   modifier
* Name:   truncate
* Purpose: Truncate a string to a certain length if necessary,
*      optionally splitting in the middle of a word, and 
*      appending the $etc string.
* Update:  change substr to msubstr and add fun msubstr.so it
*      can bu used in chinese charset.
* -------------------------------------------------------------
*/
function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false)
{
  if ($length == 0)
    return '';
  if (strlen($string) > $length) {
    $length -= strlen($etc);
    if (!$break_words)
      $string = preg_replace('//s+?(/S+)?$/', '', SubstrGB($string, 0, $length+1));
    return SubstrGB($string, 0, $length).$etc;
  } else
    return $string;
}
function SubstrGB($str,$start,$len){
  for($i=0;$i<$start+$len;$i++){
    $tmpstr=(ord($str[$i])>=161 && ord($str[$i])<=254&& ord($str[$i+1])>=161 && ord($str[$i+1])<=254)?$str[$i].$str[++$i]:$tmpstr=$str[$i];
    if ($i>=$start&&$i<($start+$len))$tmp .=$tmpstr;
  }
  return $tmp;
}
/* vim: set expandtab: */
?>

更多關(guān)于Smarty相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《smarty模板入門基礎(chǔ)教程》、《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于smarty模板的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論