I am trying to create an excerpt of a blog entry in html. I dont want the excerpt to include any images so I included a function which strips images out, which is working correctly.
I am using fckeditor which wraps images in
. So if the html was<p><img src="/yada" /> Here is some sample text. </p>
the output from the function would be
Here is some sample text.
However...If the image is the only thing inside the p tags, the function returns an empty string, which is correct, but undesirable.
I have tried to make a recursive function that passes the content, starting from the end of the empty p tags, until it finds a p tags that has content, but it just keeps returning an empty string.
function get_excerpt($content) {
$start = strpos($content, '<p');
if($start !== FALSE) {
$p_open = strpos($content, '>', $start) + 1;
$p_close = strpos($content, '</p>', $p_open);
$length = $p_close - $p_open;
$p_contents = substr($content, $p_open, $length);
$p_contents = removeImages($p_contents);
$contentLength = strlen($content);
$newContent = substr($content, $p_close);
if($p_contents == "")
{
get_excerpt($newContent);
}
else
{
return $p_contents;
}
}
else
{
return 'Excerpt not available';
}
}
function removeImages($string)
{
$newString = preg_replace("/<img[^>]+\>/i", "", $string);
return $newString;
}