Exemplo n.º 1
0
/**
 * 文本截断+url链接加上gate.baidu.com前缀,自动处理超链接的截断
 * @param  String  $str    原始内容,其中有a 标签
 * @param  integer $len    截取内容长度
 * @param  integer $urllen url截断显示的长度
 * @return String          截断后的文本
 */
function smarty_modifier_cutcontent($str, $len = 1500, $urllen = 30)
{
    //防止br被切割,先转成\n,截取后再fis_addbr中再转回来
    $str = str_replace("<br />", "\n", $str);
    //parse before cut
    $str = fis_parselink($str, $urllen);
    $lenarray = array('34', '50');
    //no need cut
    if (mb_strlen($str) < $len) {
        return fis_addbr($str);
    }
    $str = mb_substr($str, 0, $len, 'utf-8');
    //find last <a and </a> and check if it is ok
    $pos_of_a = mb_strrpos($str, '<a');
    if ($pos_of_a === false) {
        return fis_addbr($str);
    }
    $pos_of_aend = mb_strrpos($str, '</a>');
    //</a> is after <a>
    if ($pos_of_a < $pos_of_aend) {
        return fis_addbr($str);
    }
    //</a> is not after <a>
    $str = mb_substr($str, 0, $pos_of_a, 'utf-8');
    //$str = fis_addpoint($str);
    return fis_addbr($str);
}
Exemplo n.º 2
0
/**
 * 与cutcontent相对应,返回相同截断策略后剩下的内容
 * @param  String  $str   原始内容
 * @param  integer $start 起始截断offset
 * @return String         截断后的内容
 */
function smarty_modifier_leftcontent($str, $start = 1500)
{
    //防止br被切割,先转成\n,截取后再fis_addbr中再转回来
    $str = str_replace("<br />", "\n", $str);
    $str = fis_parselink($str);
    $str_right = mb_substr($str, 0, $start, 'utf-8');
    //find last <a and </a> and check if it is ok
    $pos_of_a = mb_strrpos($str_right, '<a');
    $pos_of_aend = mb_strrpos($str_right, '</a>');
    if ($pos_of_a > $pos_of_aend) {
        $str_right = mb_substr($str, 0, $pos_of_a, 'utf-8');
    }
    $str_left = mb_substr($str, mb_strlen($str_right), mb_strlen($str), 'utf-8');
    return fis_addbr($str_left);
}