HTML Excerpt with PHP

Published on by Morgan Ney

Updated on

Most blog owners would like to be able to show excerpts or previews of their articles as HTML. This allows the excerpt to be styled similarly to the post's landing page, providing a seamless transition for the reader from excerpt to full article.

Here is an example of how to create an HTML excerpt using PHP's DOMDocument class, where $html represents the entire article, and $num_chars the lower bound to the number of characters in the excerpt.

Note that this function returns a valid HTML preview of your blog's articles. I've used it as an answer on stackoverflow.

Relevant to  PHP, HTML, DOM
Get in Touch

BizzyBtyes is owned by Morgan Ney, a father, husband, and web developer originally from the San Francisco Bay Area and now living in Coarsegold, Ca. just south of Yosemite.