Home > Article > CMS Tutorial > How to use dedecms string interception function
#How to use dedecms string interception function?
dedecms cn_substr_utf8 string interception function discussion
Recommended study: 梦weavercms
This article I saw on phpsir, The main thing is that there seems to be some problems with the cn_substr_utf8 function of dedecms. Friends who study dedecms can take a look
The cn_substr_utf8 function in dedecms is like this
The code is as follows:
/** * utf-8中文截取,单字节截取模式 * * @access public * @param string $str 需要截取的字符串 * @param int $slen 截取的长度 * @param int $startdd 开始标记处 * @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 = '';</p> <p> //为了兼容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; } }
The code is as follows:
if(strlen($str) < $length + strlen($ar[0][$i]) )
One line may cause an extra character after interception. You can consider changing it to
The code is as follows:
if(strlen($str) < $length + strlen($ar[0][$i]) -1 )
The test code is as follows
The code is as follows:
$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<$str_len;$i++) { if($startdd==0) { $restr .= $c; } else if($i > $startdd) { $restr .= $c; }
if(ord($str[$i])>0x80) { if($str_len>$i+1) { $c = $str[$i].$str[$i+1]; } $i++; } else { $c = $str[$i]; }
if($i >= $enddd) { if(strlen($restr)+strlen($c)>$slen) { break; } else { $restr .= $c; break; } } } return $restr; }
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; }
function cn_substr_utf82($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]) -1 ) // phpsir 加了 -1 {
$str .= $ar[0][$i]; } else {
break; } } } return $str; }
The above is the detailed content of How to use dedecms string interception function. For more information, please follow other related articles on the PHP Chinese website!