dedecms的cn_substr()和cn_substr_utf8()截取的字符串ms不准,平时也用习惯cn_substr(),也不愿用什么cn_substr_utf8()今天弄了下,现在还是比较准了。按照一个汉字2个字节调用就行了!
方法说明:
一、找到includehelpersstring.helper.php把原来约33到102行(也就是定义cn_substr()函数的那段代码)替换掉,你要是怕不行,可以先把这个文件备份下,亲;
Copy to Clipboard引用的内容:[www.dede58.com]
/**
* 中英文截取字符串,汉字安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)’]即可
本站除原创内容,其余内容均收集自互联网,仅限用于学习和研究目的,本站不对其内容的合法性、可用性承担任何责任。本站禁止以任何形式发布或转载任何违法相关信息,若您发现请立即向站长举报;用户投稿一切资源内容不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
如有版权内容,其版权均归原作者所有,本站虽力求保存原有版权信息,但因众多资源经多次转载,已无法确定其真实来源,故敬请原作者谅解!为尊重作者劳动成果,请购买正版支持作者,谢谢!若您对「HULIKU.COM」所载资源作品版权归属存有异议,请发送邮箱:ihuliku@qq.com 进行处理。
本站资源文件大多存储至云盘,如您发现图片或链接失效,请联系站长或作者,我们将及时进行更新。资源都是经过站长或作者收集测试优化后进行发布分享。如若转载请在贵站文内以超链形式注明狐狸库文章出处,谢谢合作!












![WordPress子比主题美化教程合集[2023/04/17]-狐狸库](https://huliku.com/pic/2023032607024935.png)
![狐狸库自定义子比主题底部footer页脚美化(自适应)[06.26]更新-狐狸库](https://huliku.com/pic/2023062610013835.png)

















请登录后查看评论内容