The method of intercepting function of dedecms string
Editor to share with you the method of dedecms string interception function. I hope you will gain a lot after reading this article. Let's discuss it together.
How to use the dedecms string interception function?
Discussion on cn_substr_utf8 string interception function of dedecms
Recommended study: dream weaving cms
The article I saw in phpsir mainly said that there seems to be something wrong with the cn_substr_utf8 function of dedecms. Friends who study dedecms can read it.
In dedecms, the cn_substr_utf8 function goes like this
The code is as follows:
/ * utf-8 Chinese interception, single-byte intercept mode * * @ access public* @ param string $str string to be intercepted * @ param int $slen intercept length * @ param int $startdd start tag * @ return string*/if (! Function_exists ('cn_substr_utf8')) {function cn_substr_utf8 ($str, $length, $start=0) {if (strlen ($str))
< $start+1){return '';}preg_match_all("/./su", $str, $ar);$str = '';$tstr = ''; //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取for($i=0; isset($ar[0][$i]); $i++){if(strlen($tstr) < $start){$tstr .= $ar[0][$i];}else{if(strlen($str) < $length + strlen($ar[0][$i]) ){$str .= $ar[0][$i];}else{break;}}}return $str;}} 其中 代码如下: if(strlen($str) < $length + strlen($ar[0][$i]) ) 一行可能会造成截取后多了一个字符,可以考虑改为 代码如下: if(strlen($str) < $length + strlen($ar[0][$i]) -1 ) 测试代码如下 代码如下: $f = "你好fasdfa你fasdf#e#";$pos = strpos($f,'#e#');var_dump($pos);var_dump(cn_substr_utf8($f,$pos));var_dump(cn_substr_utf82($f,$pos)); function cn_substr($str, $slen, $startdd=0){global $cfg_soft_lang;if($cfg_soft_lang=='utf-8'){return cn_substr_utf8($str, $slen, $startdd);}$restr = '';$c = '';$str_len = strlen($str);if($str_len < $startdd+1){return '';}if($str_len < $startdd + $slen || $slen==0){$slen = $str_len - $startdd;}$enddd = $startdd + $slen - 1;for($i=0;$i $startdd){$restr .= $c;} if(ord($str[$i])>0x80) {if ($str_len > $iTun1) {$c = $str [$I]. $str [$iTun1];} $iTunes;} else {$c = $str [$I];}
If ($I > = $enddd) {if (strlen ($restr) + strlen ($c) > $slen) {break;} else {$restr. = $c restr;}} return $restr;}
Function cn_substr_utf8 ($str, $length, $start=0) {if (strlen ($str) < $start+1) {return';} preg_match_all ("/. / su", $str, $ar)
$str =''; $tstr =''
/ / in order to be compatible with the following versions of mysql4.1, consistent with database varchar, intercept for by byte is used here ($ar [0] [$I]); $ar +) {if (strlen ($tstr) < $start) {
$tstr. = $ar [0] [$I];} else {
If (strlen ($str) < $length + strlen ($ar [0] [$I])) {
$str. = $ar [0] [$I];} else {
Break;}} return $str;}
Function cn_substr_utf82 ($str, $length, $start=0) {if (strlen ($str) < $start+1) {return';} preg_match_all ("/. / su", $str, $ar)
$str =''; $tstr =''
/ / in order to be compatible with the following versions of mysql4.1, consistent with database varchar, intercept for by byte is used here ($ar [0] [$I]); $ar +) {if (strlen ($tstr) < $start) {
$tstr. = $ar [0] [$I];} else {
If (strlen ($str) < $length + strlen ($ar [0] [$I])-1) / / phpsir added-1 {
$str. = $ar [0] [$I];} else {
Break;} return $str;} after reading this article, I believe you have a certain understanding of the method of dedecms string interception function, want to know more about it, welcome to follow the industry information channel, thank you for reading!