Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying first n paragraphs of string using preg_match()

The code below shows the first paragraph of a string, but I'd like it to show the first three paragraphs:

preg_match("/<p>(.*)<\/p>/",$paragraphs,$matches);
echo $matches[0];

I'm new to regular expressions, so I've been struggling to figure this out. Any ideas?

Thanks for your help.

like image 406
user1515445 Avatar asked Jan 26 '26 14:01

user1515445


1 Answers

You can use preg_match_all:

preg_match_all("/<p>(.*)<\/p>/", $paragraphs, $matches);
echo $matches[0][0];
echo $matches[0][1];
echo $matches[0][2];

When you use preg_match_all, each subarray in $matches gains a new entry for each match.

See php.net/manual/en/function.preg-match-all.php for more info.

like image 178
Palladium Avatar answered Jan 29 '26 02:01

Palladium