0

Can anybody tell me a regular expression to use within some PHP to find the following:

  1. <p>&nbsp;</p> with any variation of white space between those tags

  2. <p><br/> again with any variation of white space between those tags

Any help appreciated, thanks!

James
  • 17
  • 2
  • 5
  • 2
    Oh god, there's so much blood http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454.... Should probably look into a parser instead. – Robert Sep 25 '10 at 17:09
  • @Robert - truly one of the most epic answers given on here... Lol! – Buggabill Sep 25 '10 at 17:13

2 Answers2

1

If you want to parse XML (or HTML) you should think about using a XML parser instead of regex. It would be more efficient.

PHP already contains an XML parser which is good for you :)


Resources :

On the same topic :

Community
  • 1
  • 1
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • Hey, thanks. It’s actually for a parameter within an ExpressionEngine find and replace plugin, which I thought would be using PHP. – James Sep 25 '10 at 17:14
  • XML parser vs. regex isn't necessarily "more efficient" depending on what the task is. It's usually "more correct"; but in this very limited case scenario, regex actually works fine (and possibly faster). – Amber Sep 25 '10 at 17:21
0

Pretty straightforward (and folks, because he's asking for things with only whitespace between them, not nested tags or attributes or any of that, a parser isn't necessary in this case...)

/<p>\s+<\/p>/

/<p>\s+<br\/>/
Amber
  • 507,862
  • 82
  • 626
  • 550