网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 dedecms重新定义cn_substr函数截取字数更准确
内容
    方法说明:
    一、找到\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 );
    }
    }
    二、全站都使用cn_substr()函数,不管你程序是gbk还是utf8;
    比如你要调用10个字(拼音汉字混杂):[field:title function='cn_substr(@me,20)']即可
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/17 20:14:24