How to extract only first two paragraph form mysql?

while($row = mysql_fetch_assoc($result))
{				 	
 $about=$row["about"];
}
 $start = strrpos($about, '<p>');
 $end = strrpos($about, '</p>', $start);

$paragraph = substr($about,$start,$end-$start );

This code gives first only one paragraph. How to get first two paragraphs?

Please help

Recommended Answers

All 2 Replies

Member Avatar for diafol
$str = "<p>This is one</p><p>This is two</p><p>This is three</p>";
$pattern="/(<p[^>]*>(.*)<\/p>){2}/isU";
preg_match($pattern,$str,$matches);
echo htmlentities($matches[0]);

The {2} gives the first two paragraphs. Change it to include the required number of p's. Unfortunately, it has to start from the first paragraph. But there again, it's only a simple example.

You could also explode on "</p><p>" and get the number of paragraphs from the ensuing array - but that's messy as you'd have to reimplode and possibly add preceding <p> (if 1st paragraph not included) and </p> if last paragraph not included

Thanks!!!
:)

$str = "<p>This is one</p><p>This is two</p><p>This is three</p>";
$pattern="/(<p[^>]*>(.*)<\/p>){2}/isU";
preg_match($pattern,$str,$matches);
echo htmlentities($matches[0]);

The {2} gives the first two paragraphs. Change it to include the required number of p's. Unfortunately, it has to start from the first paragraph. But there again, it's only a simple example.

You could also explode on "</p><p>" and get the number of paragraphs from the ensuing array - but that's messy as you'd have to reimplode and possibly add preceding <p> (if 1st paragraph not included) and </p> if last paragraph not included

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.