php 正则表达式或 |操作员 [英] php regex or | operator

查看:28
本文介绍了php 正则表达式或 |操作员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个 preg_replace:

I am doing a preg_replace:

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

这将用 "" 替换我的

    但我想替换任何
      </ul>,我不知道如何使用 |(或)字符.

      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);
      

      肯定会感谢任何帮助

      谢谢,布莱恩

      推荐答案

      您可能需要像这样对/ul 中的斜杠进行转义:

      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.)

      这篇关于php 正则表达式或 |操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆