Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php regex or | operator

Tags:

regex

php

I am doing a preg_replace:

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

That will replace my <ul> with "" but Id like to replace anything that is a <ul> or </ul>, I am not getting how to use the | (or) character though.

I try like this, but it is not working right:

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

Any help would be surely appreciated

Thanks, Bryan

like image 543
bryan sammon Avatar asked Jan 20 '11 04:01

bryan sammon


2 Answers

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

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

or do this, not sure if it'll work:

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

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

like image 128
Satya Avatar answered Oct 20 '22 01:10

Satya


You have to escape the backslash.

/<ul>|<\/ul>/

like image 34
Byron Whitlock Avatar answered Oct 20 '22 01:10

Byron Whitlock