2013-10-19 102 views
0

在博客文章预览列表中工作时,我被置于此问题的前面。 他们需要缩短内容,但不要打开任何html标签。修剪很长的html以进行预览而不会破坏html标记

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

我感谢您的帮助提前一如既往(SO结束了一个非常好的地方有这样的:-)

+0

我在工作中的应用程序是一个ASP.NET MVC3的网站,并使用C#。我没有使用任何插件,并从头开始编写博客引擎。 – Mariusz

回答

1

WordPress的问题过来已经产生内置摘录到博客平台功能,从实际的博客文章生成excerpt

您没有指定您希望用于修剪功能的语言,因此这里是Wordpress版本。它可以很容易地修改和重新使用,如果需要的话可以在Wordpress之外使用。

wp_trim_words() function reference

/** 
* 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); 
}