Php regex or | operator

I am doing preg_replace:

$pattern = '/<ul>/'; $replacement = ""; $string = "hello <ul> how are you doing </ul>"; echo preg_replace($pattern, $replacement, $string); 

This will replace my <ul> with "" , but Id would like to replace everything with <ul> or </ul> , but I don’t understand how to use the | (or).

I try like this, but it does not work correctly:

 $pattern = '/<ul>|</ul>/'; $replacement = ""; $string = "hello <ul> how are you doing </ul>"; echo preg_replace($pattern, $replacement, $string); 

Any help would certainly be appreciated.

Thanks Bryan

+6
php regex
source share
2 answers

You may need to avoid the slash in / ul, like this:

 $pattern = '/<ul>|<\/ul>/'; 

or do it, not sure if this will work:

 $pattern = '/<\/?ul>/'; 

("?" means zero or one of the previous characters, which in this case is equal to "/".)

+13
source share

You need to avoid the backslash.

/<ul>|<\/ul>/

+1
source share

All Articles