Extracting the first paragraph of a content

Hi,
I would like to extract the first paragraph of a content from a database, the code i got it from somewhere works fine only if i have more than one paragraph in the content, but it display of nothing if the content only have one paragraph…

<?php echo substr($row_feeds['pg_content'], 0, strpos($row_feeds['pg_content'], "\
"));?>

how can i get around to this tricky part…?

Thanks,
Lau

Check the result of strpos. If it’s false, show the entire content, otherwise use the substr from your code.

How about:

list($paragraph) = explode("\
", $row_feeds['pg_content']);

thanks for this tips, sorry i should have said that this is how my content would look like in the database,

<p>xxxxx</p>
<p>xxxxx</p>
<p>xxxxx</p>

so i just changed the code to </p>,

<?php echo substr($row_feeds['pg_content'], 0, strpos($row_feeds['pg_content'], "</p>"));?>

But, this is just to extract first paragraph from the content - how about if i want to extract first TWO paragraphs from the content??

Thanks,
Lau

Assuming you have no attributes on the P element (id, class etc) this would work.

<?php
function limitParagraphs($sHTML, $iLimit)
{
    if(1 === preg_match('~(<p>.+?</p>){' . (int)$iLimit . '}~i', $sHTML, $aMatches))
    {
        return $aMatches[0];
    }
    return $sHTML;
}

echo limitParagraphs('<p>cheese</p><p>bacon</p><p>eggs</p><p>cheese</p><p>bacon</p><p>eggs</p>', 1); #<p>cheese</p>
echo limitParagraphs('<p>cheese</p><p>bacon</p><p>eggs</p><p>cheese</p><p>bacon</p><p>eggs</p>', 3); #<p>cheese</p><p>bacon</p><p>eggs</p>
echo limitParagraphs('<p>cheese</p><p>bacon</p><p>eggs</p><p>cheese</p><p>bacon</p><p>eggs</p>', 2); #<p>cheese</p><p>bacon</p>
?>

*please forgive my shoddy RegExp.

thank you for the code and advice. this is my stupid solution… lol

$paragraph = explode("</p>", $row_feeds['pg_content']);
	if (empty($paragraph[1]))
		{
		$content = $paragraph[0]."</p>";
		}
	else
		{
		$content = $paragraph[0]."</p>".$paragraph[1]."... <a href='$pg_title_clean'>more &raquo;</a>"."</p>" ;
		}
$numMatches = preg_match_all('|<p>(.+?)</p>|', $row_feeds['pg_content'], $matches);

Now all of your paragraphs are in $matches[1], as an array. The first paragraph is $matches[1][0].

this is great. i know how to manipulate the data from here now. thanks! :smiley: