dedecms怎么重新定义cn_substr函数截取字数更准确?
dedecms的cn_substr()和cn_substr_utf8()截取的字符串ms不准,平时也用习惯cn_substr(),也不愿用什么cn_substr_utf8()今天弄了下,现在还是比较准了。按照一个汉字2个字节调用就行了
推荐学习:织梦cms
方法说明:
一、找到includehelpersstring.helper.php把原来约33到102行(也就是定义cn_substr()函数的那段代码)替换掉,你要是怕不行,可以先把这个文件备份下,亲;
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* 中英文截取字符串,汉字安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 ( “/(|
|
|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 = $cutLen)break;
}
if ($noc > $cutLen) $n -= $tn;
$nStr = substr ( $str, 0, $n );
} else {
for($i = 0; $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)]即可
以上就是dedecms怎么重新定义cn_substr函数截取字数更准确的详细内容,更多请关注php中文网其它相关文章!