溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

dedecms重新定義cn_substr函數(shù)截取字?jǐn)?shù)的方法

發(fā)布時間:2020-08-26 10:00:12 來源:億速云 閱讀:101 作者:小新 欄目:建站服務(wù)器

小編給大家分享一下dedecms重新定義cn_substr函數(shù)截取字?jǐn)?shù)的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

dedecms怎么重新定義cn_substr函數(shù)截取字?jǐn)?shù)更準(zhǔn)確?

dedecms的cn_substr()和cn_substr_utf8()截取的字符串ms不準(zhǔn),平時也用習(xí)慣cn_substr(),也不愿用什么cn_substr_utf8()今天弄了下,現(xiàn)在還是比較準(zhǔn)了。按照一個漢字2個字節(jié)調(diào)用就行了

推薦學(xué)習(xí):織夢cms

方法說明:

一、找到\include\helpers\string.helper.php把原來約33到102行(也就是定義cn_substr()函數(shù)的那段代碼)替換掉,你要是怕不行,可以先把這個文件備份下,親;

代碼如下:

/** 
* 中英文截取字符串,漢字安2個字節(jié) 
* 
* @access public 
* @param string $str 需要截取的字符串 
* @param int $cutLen 截取的長度 
* @param bool $cutSlashes 是否去掉\ 
* @param bool $addSlashes 是加\ 
* @param string $oDot 截取后加的字符串,如經(jīng)常用的三個點 
* @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()函數(shù),不管你程序是gbk還是utf8;

比如你要調(diào)用10個字(拼音漢字混雜):[field:title function='cn_substr(@me,20)']即可

看完了這篇文章,相信你對dedecms重新定義cn_substr函數(shù)截取字?jǐn)?shù)的方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI