Jump to content

Simple preg match with <p> tags


a1amattyj

Recommended Posts

Hello,

 

Trying to get all content within paragraph tags. Im using:

 

preg_match("#<p>(.*)</p>#i", $contents, $match);

 

There are around 8 paragraphs on this page, but it only matches two - and thats the first <p> tag with the last </p> tag. Not to sure what I should change (.*) too?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/241410-simple-preg-match-with-tags/
Share on other sites

Well, you also have the problem that your regex is "greedy". Even if you used preg_match_all() it would only return one result - everything from the very first "<p>" to the very last "</p>". You need to make the expression "non-greedy" so that it will return each instance of "<p>" to the next "</p>". It is the asterisk (*) that is behaving in a greedy manner in that expression. One way to make it non-greedy is to add a question mark (?) after the asterisk.

 

Also, I think you have to escape the forward slash. And I would also add some handling for the opening paragraph tag in case there are additional attributes for the paragraph tag such as a class or style attribute.

 

Try this:

preg_match_all("#<p[^>]*>(.*?)<\/p>#i", $contents, $matches);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.