ホームページ  >  記事  >  CMS チュートリアル  >  dedecms が cn_substr 関数を再定義して単語数をより正確にインターセプトする方法

dedecms が cn_substr 関数を再定義して単語数をより正確にインターセプトする方法

藏色散人
藏色散人オリジナル
2019-12-25 09:54:301823ブラウズ

dedecms が cn_substr 関数を再定義して単語数をより正確にインターセプトする方法

dedecms単語数をより正確に取得するために cn_substr 関数を再定義するにはどうすればよいですか?

dedecms の cn_substr() と cn_substr_utf8() は文字列 ms をインターセプトします。通常は cn_substr() を使用しますが、cn_substr_utf8() は使いたくないです。今日試してみましたが、まだです。今は良くなりました。漢字 2 バイトに従って呼び出してください。

推奨学習:梦weavercms

メソッドの説明:

一, \include\helpers\string.helper.php を見つけて、元の 33 行目から 102 行目 (つまり、cn_substr() 関数を定義するコード) を置き換えます。心配な場合は、最初にこのファイルをバックアップしても構いません。

コードは次のとおりです:

/** 
* 中英文截取字符串,汉字安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 ); 
} 
}

2。プログラムが gbk か utf8 かに関係なく、サイト全体で cn_substr() 関数が使用されます。

たとえば、 10 文字 (ピンイン混合中国語文字) を呼び出したい場合: [field:title function='cn_substr(@me,20)'] can

以上がdedecms が cn_substr 関数を再定義して単語数をより正確にインターセプトする方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。