2013-10-19 29 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); 
}