2011-03-05 81 views

回答

2

最好的辦法是使用strip_tags()從中刪除HTML,並使用substr僅顯示前300個字符。否則,您必須解析HTML以在適當的位置將其分解,以免打破其他佈局。

2

用strip_tags()換行()

<?php 
$blog_entry = '<div class="myclass"><p><h1>I am trying to write a blog system.</h1> The main page is consist of part of the content of blog entries.</p>  
<p>The problem is how could I make sure the excerpt is truncated correctly, since the blog entries is stored in HTML code.</p> 
<p>Thanks.</p></div>'; 

// Allow a couple of tags (<p>,<a>), or don't - wrap excerpts into your own CSS class in your UI 

$thisExcerpt = wordwrap(strip_tags($blog_entry, '<p>,<a>'),50); 
$thisExcerpt = explode("\n", $thisExcerpt); 
$thisExcerpt = $thisExcerpt[0]; 

echo $thisExcerpt . '...'; 
?> 

輸出:

I am trying to write a blog system. The main...