【问题标题】:How to use Multiple Delimiter in preg_split()如何在 preg_split() 中使用多个分隔符
【发布时间】:2015-08-22 07:41:59
【问题描述】:

我有这个 preg_split 函数和搜索任何<br>的模式

不过,除了<br>,我还想为它添加更多模式。

如何使用下面的当前代码行来做到这一点?

preg_split('/<br[^>]*>/i', $string, 25);

提前致谢!

【问题讨论】:

  • PHPs preg_split() 函数只接受一个模式参数,而不是多个。所以你必须使用正则表达式的力量来匹配你的分隔符。

标签: php delimiter preg-split


【解决方案1】:

我不能评论这就是为什么我要回答, 告诉我你需要实施什么\, 或使用PHP live regex creator 之类的网站

【讨论】:

    【解决方案2】:

    PHP 的 preg_split() 函数只接受一个模式参数,而不是多个。所以你必须使用正则表达式的力量来匹配你的分隔符。

    这是一个例子:

    preg_split('/(<br[^>]*>)|(<p[^>]*>)/i', $string, 25);
    

    如果匹配 html 换行符 和/或 段落标签。

    使用正则表达式工具来测试表达式很有帮助。本地服务或基于 Web 的服务,例如 https://regex101.com/

    以上省略示例文字

    this is a <br> text
    with line breaks <br /> and
    stuff like <p>, correct?
    

    像这样:

    Array
    (
        [0] => this is a
        [1] =>  text
    with line breaks
        [2] =>  and
    stuff like
        [3] => , correct?
    )
    

    但是请注意,对于解析 html 标记,DOM 解析器可能是更好的选择。您不会冒险绊倒转义字符等......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-07
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      相关资源
      最近更新 更多