php 正则表达式或 | 操作员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4743329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php regex or | operator
提问by bryan sammon
I am doing a preg_replace:
我正在做一个 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.
这将替换 my <ul>
with""
但我想替换任何 a <ul>
or </ul>
,但我不知道如何使用|
(或)字符。
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
谢谢,布莱恩
回答by Satya
You may need to escape the slash in /ul like this:
您可能需要像这样转义 /ul 中的斜杠:
$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.)
('?' 表示前一个字符的零或一个,在本例中为 '/'。)
回答by Byron Whitlock
You have to escape the backslash.
你必须逃避反斜杠。
/<ul>|<\/ul>/
/<ul>|<\/ul>/