<?php/** * Smarty plugin * @package Smarty * @subpackage plugins *//** * Smarty truncate modifier plugin * * Type: modifier<br> * Name: truncate<br> * Purpose: Truncate a string to a certain length if necessary, * optionally splitting in the middle of a word, and * appending the $etc string. * @link http://smarty.php.net/manual/en/language.modifier.truncate.php * truncate (Smarty online manual) * @param string * @param integer * @param string * @param boolean * @return string */function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false){ if ($length == 0) return ''; if (c_strlen($string) > $length) { $length -= c_strlen($etc);// if (!$break_words)// $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));// return substr($string, 0, $length).$etc; return c_substr($string, 0, $length).$etc; } else return $string;}/* vim: set expandtab: */function c_substr($str,$start=0) { preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $str, $ar); if(func_num_args() >= 3) { $end = func_get_arg(2); return join("",array_slice($ar[0],$start,$end)); } else { return join("",array_slice($ar[0],$start)); } }function c_strlen($str) { return preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $dummy);}?>
// add Multi-Byte substr. for xiami at www.it580.com/bbsfunction msubstr($str,$start,$len){$strlen=$start+$len;for($i=0;$i<$strlen;$i++){if(ord(substr($str,$i,1))>0xa0){$strtmp.=substr($str,$i,2);$i++;}else{$strtmp.=substr($str,$i,1);}}return $strtmp;}
function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false){ if ($length == 0) return ''; if (mb_strlen($string) > $length) { $length -= mb_strlen($etc); if (!$break_words) $string = preg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length+1)); return mb_substr($string, 0, $length).$etc; } else return $string;}