dedecms怎么重新定义cn_substr函数截取字数更准确
dedecms怎么重新定义cn_substr函数截取字数更准确?
dedecms的cn_substr()和cn_substr_utf8()截取的字符串ms不准,平时也用习惯cn_substr(),也不愿用什么cn_substr_utf8()今天弄了下,现在还是比较准了。按照一个汉字2个字节调用就行了
方法说明:
一、找到\include\helpers\string.helper.php把原来约33到102行(也就是定义cn_substr()函数的那段代码)替换掉,你要是怕不行,可以先把这个文件备份下,亲;
代码如下:
/**
* 中英文截取字符串,汉字安2个字节
*
* @access public
* @param string $str 需要截取的字符串
* @param int $cutLen 截取的长度
* @param bool $cutSlashes 是否去掉\
* @param bool $addSlashes 是加\
* @param string $oDot 截取后加的字符串,如经常用的三个点
* @param bool $hasHtml 是否有html
* @return string
*/
if ( ! function_exists(‘cn_substr’)){
function cn_substr($str, $cutLen, $oDot = null, $hasHtml = false, $cutSlashes = false, $addSlashes = false) {
global $cfg_soft_lang;
$str = trim ( $str );
if ($cutSlashes) $str = stripslashes ( $str );
if($hasHtml){
$str = preg_replace ( “/(\<[^\<]*\>|\r|\n|\s|\[. ?\])/is”, ‘ ‘, $str );
$str = htmlspecialchars ( $str );
}else{
$str = htmlspecialchars ( $str );
}
if ($cutLen && strlen ( $str ) > $cutLen) {
$nStr = ”;
if ($cfg_soft_lang == ‘utf-8′) {
$n = 0;
$tn = 0;
$noc = 0;
while ( $n < strlen ( $str ) ) {
$t = ord ( $str [$n] );
if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1;
$n ;
$noc ;
} elseif (194 <= $t && $t <= 223) {
$tn = 2;
$n = 2;
$noc = 2;
} elseif (224 <= $t && $t < 239) {
$tn = 3;
$n = 3;
$noc = 2;
} elseif (240 <= $t && $t <= 247) {
$tn = 4;
$n = 4;
$noc = 2;
} elseif (248 <= $t && $t <= 251) {
$tn = 5;
$n = 5;
$noc = 2;
} elseif ($t == 252 || $t == 253) {
$tn = 6;
$n = 6;
$noc = 2;
} else {
$n ;
}
if ($noc >= $cutLen)break;
}
if ($noc > $cutLen) $n -= $tn;
$nStr = substr ( $str, 0, $n );
} else {
for($i = 0; $i < $cutLen – 1; $i ) {
if (ord ( $str [$i] ) > 127) {
$nStr .= $str [$i] . $str [$i 1];
$i ;
} else {
$nStr .= $str [$i];
}
}
}
$str = $nStr . $oDot;
}
if ($addSlashes) $str = addslashes ( $str );
$str = htmlspecialchars_decode ( $str );
return trim ( $str );
}
}
二、全站都使用cn_substr()函数,不管你程序是gbk还是utf8;
比如你要调用10个字(拼音汉字混杂):[field:title function='cn_substr(@me,20)']即可
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tangbgkhk
系列文章
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01