A solution I came up with is similar to jheddings method.
I corrected his script up and used a code snippet I found here
Snipplr Close Tags In A HTML Snippet
To find open tags and close them (Note that I am assuming you really only care about closing p tags)
Note: The snippet may have shortcomings but it managed to get the job done for the example I was working with
So in the example script below I am taking the sample blurb cutting out everything after the break marker and appending "..." to it. Then we strip_tag everything except the p tags. Then I am using the closetags function to match all tags and close any that are unmatched.
It is far from neat but if your data set is simple enough it may be a quick way to go about it.
<?php
$project_blurb = "<p>This is a blurb with content</p><p>This is another<!-- break -->blurb</p>";
if ($pos = strpos($project_blurb, '<!-- break -->')) {
$project_desc = substr($project_blurb, 0, $pos)."...";
} else {
$project_desc = $project_blurb;
}
$project_desc = strip_tags($project_desc, '<p>');
$project_desc = closetags($project_desc);
echo $project_desc;
function closetags ( $html )
{
#put all opened tags into an array
preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU", $html, $result );
$openedtags = $result[1];
#put all closed tags into an array
preg_match_all ( "#</([a-z]+)>#iU", $html, $result );
$closedtags = $result[1];
$len_opened = count ( $openedtags );
# all tags are closed
if( count ( $closedtags ) == $len_opened )
{
return $html;
}
$openedtags = array_reverse ( $openedtags );
# close tags
for( $i = 0; $i < $len_opened; $i++ )
{
if ( !in_array ( $openedtags[$i], $closedtags ) )
{
$html .= "</" . $openedtags[$i] . ">";
}
else
{
unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
}
}
return $html;
}
?>