I am trying to write a blog system. The main page is consist of part of the content of blog entries.
The problem is how could I make sure the excerpt is truncated correctly, since the blog entries is stored in HTML code.
Thanks.
Your best bet would be to use strip_tags()
to remove the HTML from it and show only the first 300 or so characters using substr
. Otherwise you'd have to parse the HTML to break it at an appropriate place so as not to break the rest of your layout.
strip_tags() and wordwrap()
<?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("
", $thisExcerpt);
$thisExcerpt = $thisExcerpt[0];
echo $thisExcerpt . '...';
?>
Outputs :
I am trying to write a blog system. The main...