截断不同长度的信息

时间:2011-03-31 作者:Zach Shallbetter

不久前,我得到了一种自定义截断形式的帮助Toscho 在…上this post 它工作得很好,但我发现我需要为不同的内容创建多个长度变化。例如,在我的支持页面上,我需要80个max\\u字符,而在我的主页上,我需要200个max\\u字符。

我尝试添加此功能的第二个实例,但毫不奇怪,它完全扼杀了我网站的其他方面。

一如既往,我们将不胜感激。谢谢大家!

function utf8_truncate( $string, $max_chars = 200, $append = "\\xC2\\xA0…" )
{
    $string = strip_tags( $string );
    $string = html_entity_decode( $string, ENT_QUOTES, \'utf-8\' );
    // \\xC2\\xA0 is the no-break space
    $string = trim( $string, "\\n\\r\\t .-;–,—\\xC2\\xA0" );
    $length = strlen( utf8_decode( $string ) );

    // Nothing to do.
    if ( $length < $max_chars )
    {
        return $string;
    }

    // mb_substr() is in /wp-includes/compat.php as a fallback if
    // your the current PHP installation doesn\'t have it.
    $string = mb_substr( $string, 0, $max_chars, \'utf-8\' );

    // No white space. One long word or chinese/korean/japanese text.
    if ( FALSE === strpos( $string, \' \' ) )
    {
        return $string . $append;
    }

    // Avoid breaks within words. Find the last white space.
    if ( extension_loaded( \'mbstring\' ) )
    {
        $pos   = mb_strrpos( $string, \' \', \'utf-8\' );
        $short = mb_substr( $string, 0, $pos, \'utf-8\' );
    }
    else
    {
        // Workaround. May be slow on long strings.
        $words = explode( \' \', $string );
        // Drop the last word.
        array_pop( $words );
        $short = implode( \' \', $words );
    }

    return $short . $append;
}

1 个回复
最合适的回答,由SO网友:kaiser 整理而成

我知道toscho不太喜欢这样,但无论如何:将输入参数转换为数组:

function utf8_truncate( $args = array( \'string\' => null, \'max_chars\' => 200, \'append\' => "\\xC2\\xA0…" ) )
{
    $args[\'string\'] = strip_tags( $args[\'string\'] );
    $args[\'string\'] = html_entity_decode( $args[\'string\'], ENT_QUOTES, \'utf-8\' );
    // \\xC2\\xA0 is the no-break space
    $args[\'string\'] = trim( $args[\'string\'], "\\n\\r\\t .-;–,—\\xC2\\xA0" );
    $length = strlen( utf8_decode( $args[\'string\'] ) );

    // Nothing to do.
    if ( $length < $args[\'max_chars\'] )
    {
        return $args[\'string\'];
    }

    // mb_substr() is in /wp-includes/compat.php as a fallback if
    // your the current PHP installation doesn\'t have it.
    $args[\'string\'] = mb_substr( $args[\'string\'], 0, $args[\'max_chars\'], \'utf-8\' );

    // No white space. One long word or chinese/korean/japanese text.
    if ( FALSE === strpos( $args[\'string\'], \' \' ) )
    {
        return $args[\'string\'] . $args[\'append\'];
    }

    // Avoid breaks within words. Find the last white space.
    if ( extension_loaded( \'mbstring\' ) )
    {
        $pos   = mb_strrpos( $args[\'string\'], \' \', \'utf-8\' );
        $short = mb_substr( $args[\'string\'], 0, $pos, \'utf-8\' );
    }
    else
    {
        // Workaround. May be slow on long strings.
        $words = explode( \' \', $args[\'string\'] );
        // Drop the last word.
        array_pop( $words );
        $short = implode( \' \', $words );
    }

    return $short . $args[\'append\'];
}
这样就可以像这样使用它(you possibily missed how to use arrays anyway):

$args = array(
     \'string\' => \'bla\'
    ,\'max_chars\' => 50 // INPUT LENGTH HERE
);
echo \'<p>\' . utf8_truncate( $args ) . \'</p>\';
您还可以根据需要切换此选项:

if ( is_page() )
{
    $args[\'max_chars\'] = 100;
}
elseif ( is_archive() )
{
    $args[\'max_chars\'] = 50;
}
elseif ( is_whatever() )
    ... etc ...
}

结束

相关推荐

Automating Excerpt

我正在努力automate 编辑通过自动化工作excerpts.我的解决方案可行,但几乎没有问题:如果一篇文章开头有图像/破坏的html,它会破坏版面。子字符串剪切单词。是否有更好的解决方案来自动化摘录或改进现有代码?<?php if(!empty($post->post_excerpt)) { the_excerpt(); } else { echo \"<p>\".substr(get_the