首页 > 解决方案 > 使用 PHP 在 html 字符串中删除 AMP 页面的自定义标签

问题描述

我在编写正则表达式方面不是那么好,所以我寻求帮助。

我有我的页面的 html 字符串。我想将 html 字符串转换为 Google AMP 兼容页面。我想在其中替换一些自定义 html 标签。

<define:dos> This string explains DOS.</define:dos>

并且输出应该只有This string explains DOS.

我的 html 字符串中有几个字符串,从我尝试通过为每个按预期工作的标签<define:单独编写来删除它们:preg_replace

$html = preg_replace('#<define:wlan>#i', '', $html);
$html = preg_replace('#<define:wifi>#i', '', $html);
$html = preg_replace('#<define:dos>#i', '', $html);

等等

我尝试了以下方法但没有奏效:(

$html = preg_replace('#\<define[^\]>*\](.*?)\</define\>#m', "$1", $html);

我想要通用的解决方案。请帮忙。

先感谢您。

标签: phpregexpreg-replacegoogle-amp

解决方案


您可以使用

<(define:[^>]+>)(.*?)<\/\1

并替换为第二个捕获的组,\2.

https://regex101.com/r/wZQBuM/2


推荐阅读