smarty中改進(jìn)truncate使其支持中文的方法
本文實例講述了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)文章
打造超酷的PHP數(shù)據(jù)餅圖效果實現(xiàn)代碼
打造超酷的PHP數(shù)據(jù)餅圖效果實現(xiàn)代碼,比較適合做統(tǒng)計效果輸出,需要的朋友可以參考下。很多年前的代碼。2011-11-11Codeigniter中mkdir創(chuàng)建目錄遇到權(quán)限問題和解決方法
這篇文章主要介紹了Codeigniter中mkdir創(chuàng)建目錄遇到權(quán)限問題和解決方法,需要的朋友可以參考下2014-07-07php實現(xiàn)兼容2038年后Unix時間戳轉(zhuǎn)換函數(shù)
這篇文章主要介紹了php實現(xiàn)兼容2038年后Unix時間戳轉(zhuǎn)換函數(shù),使用方法和就的函數(shù)一樣,非常實用,推薦給大家,希望大家能夠喜歡。2015-03-03PHPStudy hosts文件可能不存在或被阻止打開及同步hosts失敗問題
這篇文章主要介紹了PHPStudy hosts文件可能不存在或被阻止打開,同步hosts失敗,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05laravel 解決強制跳轉(zhuǎn) https的問題
今天小編就為大家分享一篇laravel 解決強制跳轉(zhuǎn) https的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10