php - RegEx - matching space in HTML string? -


to sanitise user input wysiwyg editor, i'm trying find following strings:

<p>&nbsp;</p>  <p> </p>  <p></p> 

this regular expression i'm using:

/\<p\>([nbsp\;]*|[\s]*|[ ]*)\<\/p\>/i 

i'm quite new regex, understand, this:

  1. \<p\>: - matches <p> exactly, then
  2. ( - matches either:
    • [nbsp\;]* - "nbsp;" exactly, number of times
    • |[\s]* - or whitespace character, number of times
    • |[ ]* - or " " (a space), number of times
  3. <\/p\> - matches </p> exactly

however, this expression matches <p>nbsp;</p> , not other two.

i have tried:

/\<p\>[nbsp\;|\s| ]*\<\/p\>/i 

i testing using regex101.com (first expression, second expression)

how can work?

you can't use "whole words" inside of character class, following suffice ...

~<p>(?:&nbsp;|\s)*</p>~i 

note: don't need include <space>, \s token match whitespace , don't need escape < , >, not considered special characters.


Comments