smarty中改进truncate使其支持中文的方法
网络编程 2021-07-05 09:10www.168986.cn编程入门
这篇文章主要介绍了smarty中改进truncate使其支持中文的方法,涉及针对Smarty源码中truncate源文件进行函数功能扩展的相关技巧,需要的朋友可以参考下
本文实例讲述了smarty中改进truncate使其支持中文的方法。分享给大家供大家参考,具体如下:
smarty的truncate不支持中文的截取。将smarty目录下plugins中的modifier.truncate.php改成狼蚁网站SEO优化这个样子就可以了
<?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: */ ?>
更多关于Smarty相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。
上一篇:php求圆周率的简单实现方法 原创
下一篇:php实现的一段简单概率相关代码
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程