0

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

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by vizz
0
$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

Edited by diafol: n/a

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.