婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 網站建設 > 建站知識 > dedecms重新定義cn_substr函數截取字數準確

dedecms重新定義cn_substr函數截取字數準確

POST TIME:2021-05-24 02:51

dedecms的cn_substr()和cn_substr_utf8()截取的字符串ms不準,平時也用習慣cn_substr(),也不愿用什么cn_substr_utf8()今天弄了下,現在還是比較準了。按照一個漢字2個字節調用就行了!

方法說明: 
一、找到\include\helpers\string.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)']即可



收縮
  • 微信客服
  • 微信二維碼
  • 電話咨詢

  • 400-1100-266
主站蜘蛛池模板: 宝丰县| 酉阳| 万荣县| 黔西| 仁化县| 金塔县| 江安县| 江都市| 射阳县| 抚顺市| 东山县| 乌拉特中旗| 南投市| 桃园县| 集安市| 黑山县| 明光市| 南郑县| 方城县| 益阳市| 铜山县| 察雅县| 波密县| 平南县| 柘荣县| 新宁县| 宾阳县| 讷河市| 江西省| 阿拉善右旗| 九龙城区| 顺义区| 雷波县| 乌兰察布市| 响水县| 新密市| 泸水县| 阿瓦提县| 余庆县| 唐海县| 平定县|