Skip to content Skip to sidebar Skip to footer

Close Tags From A Truncated Html String

I have inherited a site with a news section that displays a summary of the news article. For whatever reason the creators decided that displaying the first X characters of the arti

Solution 1:

The best thing is probably to find a better algorithm for generating the excerpt, for example by running strip_tags before the truncation.

How will you otherwise handle hard-to-find-programmatically errors such as <p>What a mighty fine and warm <a href="htt or <p>His name was &quot;Emil&qu?

Solution 2:

Have you taken a look at Tidy?

Example:

$options = array("show-body-only" => true); 
$tidy = tidy_parse_string("<B>Hello</I> How are <U> you?</B>", $options);
tidy_clean_repair($tidy);
echo$tidy;

Outputs:

<b>Hello</b> How are <u>you?</u>

Solution 3:

I would install the PHP bindings for Tidy. You can then use this to clean up an HTML fragment using the following code:

<?php$fragment = '<p>What a mighty fine <a href="blah">da';

$tidy = new tidy();

$tidy->parseString($fragment,array('show-body-only'=>true),'utf8');
$tidy->cleanRepair();

echo$tidy;

Post a Comment for "Close Tags From A Truncated Html String"