【问题标题】:How remove multiple substring from two tags如何从两个标签中删除多个子字符串
【发布时间】:2017-09-16 21:18:32
【问题描述】:

给出一个这样形成的字符串:

hello Rose <tag> Micheal </tag> and <tag> July </tag> John. 

我想删除 &lt;tag&gt;&lt;/tag&gt; 之间的所有内容并输出:

hello Rose and John. 

怎么办?

【问题讨论】:

  • 你已经尝试了什么?
  • 我在这里找到了一个名为:delete_all_between 的函数,但它只适用于第一次出现,但不适用于所有。

标签: php string


【解决方案1】:

我猜你可以使用:

$new_html = preg_replace('%<tag>.*?</tag> ?%si', '', $old_html);

【讨论】:

  • 工作!但是有一个错误。当字符串以 something 结尾时,它不起作用;如何解决?
  • 把空间optional?,我会更新答案。
【解决方案2】:

如果您不想使用DOM,您可以随时使用preg_replace 来执行此操作。

$content = 'hello Rose <tag> Micheal </tag> and <tag> July </tag> John.';

$content = preg_replace('/<tag>[^<]+<\/tag>/i', '', $content);

print $content; // returns: hello Rose and John.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    相关资源
    最近更新 更多