+ -
当前位置:首页 → 问答吧 → TP2.1官方扩展函数msubstr使用问题求助?

TP2.1官方扩展函数msubstr使用问题求助?

时间:2011-08-29

来源:互联网

function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
    if(function_exists("mb_substr"))
        return mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        return iconv_substr($str,$start,$length,$charset);
    }
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
    if($suffix) return $slice."…";
    return $slice;
}


在模板中调用格式{$vo.content|msubstr=0,100}没有"......"
求解?if($suffix) return $slice."…";
也就是这一句没有执行??????

作者: onezhi   发布时间: 2011-08-29



在此函数基本上我想替换过内容中的所有html代码。怎么写比较好呢,我按照下边的方法,没有去除html代码,请教应怎么写
正则代码?
//过滤换行符
        $slice        =        preg_replace('/\r?\n/','',$slice);
        //br
        $slice        =        preg_replace('/<br(\s\/)?'.'>/i','[br]',$slice);
        $slice        =        preg_replace('/(\[br\]\s*){10,}/i','[br]',$slice);
        //过滤多余空格
//        $slice        =        str_replace('  ',' ',$slice);
        $slice        =        preg_replace('/<p>/i',' ',$slice);
        $slice        =        preg_replace('/</p>/i',' ',$slice);

或者,我按下边的写法也没有执行,为什么
$slice        =        preg_replace('/<(.*)>/i',' ',$slice);


高手指点

function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
    if(function_exists("mb_substr"))
        return mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        return iconv_substr($str,$start,$length,$charset);
    }
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
        
        //过滤换行符
        $slice        =        preg_replace('/\r?\n/','',$slice);
        //br
        $slice        =        preg_replace('/<br(\s\/)?'.'>/i','[br]',$slice);
        $slice        =        preg_replace('/(\[br\]\s*){10,}/i','[br]',$slice);
        //过滤多余空格
//        $slice        =        str_replace('  ',' ',$slice);
        $slice        =        preg_replace('/<p>/i',' ',$slice);
        $slice        =        preg_replace('/</p>/i',' ',$slice);
        
    if($suffix) return $slice."…";
    return $slice;
}

作者: onezhi   发布时间: 2011-08-29