在不破坏html标签的情况下修剪长html进行预览

时间:2020-11-27 21:36:46

I was put in front of this problem when working on a blog post preview list. They need to shorten the content but not break any html tags by leaving them open.

在处理博客帖子预览列表时,我被置于此问题的前面。他们需要缩短内容,但不要打开任何html标签。

I have heard that reg ex is not a good option. I am looking for something simple and working.

我听说reg ex不是一个好选择。我正在寻找一些简单而有效的东西。

I appreciate your help in advance as always (SO ended up being a very nice place to come over with problems like that :-)

我一如既往地感谢你的帮助(最终成为一个非常好的地方来解决这样的问题:-)

1 个解决方案

#1


1  

Wordpress has a function for generating excerpts built-in to the blogging platform which generates an excerpt from the actual blog post.

Wordpress具有生成内置于博客平台的摘录的功能,该摘录生成来自实际博客帖子的摘录。

You didn't specify which language you were looking to use for the trim function so here is the Wordpress version. It can be easily modified and re-purposed to use outside of Wordpress if need be.

你没有指定你想要用于修剪函数的语言,所以这里是Wordpress版本。如果需要,它可以很容易地修改和重新使用以在Wordpress之外使用。

wp_trim_words() function reference

wp_trim_words()函数引用

/**
 * Generates an excerpt from the content, if needed.
 *
 * The excerpt word amount will be 55 words and if the amount is greater than
 * that, then the string ' […]' will be appended to the excerpt. If the string
 * is less than 55 words, then the content will be returned as is.
 *
 * The 55 word limit can be modified by plugins/themes using the excerpt_length filter
 * The ' […]' string can be modified by plugins/themes using the excerpt_more filter
 *
 * @since 1.5.0
 *
 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
 * @return string The excerpt.
 */
function wp_trim_excerpt($text = '') {
    $raw_excerpt = $text;
    if ( '' == $text ) {
        $text = get_the_content('');

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = apply_filters('excerpt_length', 55);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[…]');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

#1


1  

Wordpress has a function for generating excerpts built-in to the blogging platform which generates an excerpt from the actual blog post.

Wordpress具有生成内置于博客平台的摘录的功能,该摘录生成来自实际博客帖子的摘录。

You didn't specify which language you were looking to use for the trim function so here is the Wordpress version. It can be easily modified and re-purposed to use outside of Wordpress if need be.

你没有指定你想要用于修剪函数的语言,所以这里是Wordpress版本。如果需要,它可以很容易地修改和重新使用以在Wordpress之外使用。

wp_trim_words() function reference

wp_trim_words()函数引用

/**
 * Generates an excerpt from the content, if needed.
 *
 * The excerpt word amount will be 55 words and if the amount is greater than
 * that, then the string ' […]' will be appended to the excerpt. If the string
 * is less than 55 words, then the content will be returned as is.
 *
 * The 55 word limit can be modified by plugins/themes using the excerpt_length filter
 * The ' […]' string can be modified by plugins/themes using the excerpt_more filter
 *
 * @since 1.5.0
 *
 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
 * @return string The excerpt.
 */
function wp_trim_excerpt($text = '') {
    $raw_excerpt = $text;
    if ( '' == $text ) {
        $text = get_the_content('');

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = apply_filters('excerpt_length', 55);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[…]');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}