如何去除 xml 文件中的 <a> 标签?

How to strip <a> tags in an xml file?

我在 xml 文件中有一个 <a> 标签。我只想去除 <a> 个标签。

例如:

<test>This is a line with <a name="idp173968"></a> tags in it</test>

我无法str_replace替换标签,因为<a>标签属性不同。

我试过了:

preg_replace("/<a[^>]+\>/i", "", $content);

如果标签的结构像这样 <a name="idp173968"/>,它工作正常。

那么,在我的案例中如何去除标签?

预期输出:

<test>This is a line with tags in it</test>

您可以尝试一个非常简单的正则表达式,例如

<a\s.*?<\/a>

Regex Demo

例子

echo preg_replace("/<a\s.*?<\/a>/i", "", $content);
=> <test>This is a line with  tags in it</test>

$content="<test>This is a line with <a name=\"idp173968\"></a> tags in it</test><article-meta>asdf</article-meta>";
echo preg_replace("/<a\s.*?<\/a>/i", "", $content);
=><test>This is a line with  tags in it</test><article-meta>asdf</article-meta>
<?php
$string = '<test>This is a line with <a name="idp173968"></a> tags in it</test>';
$pattern = '/<a\s.*?<\/a>/i';
echo preg_replace($pattern, "", $string);
?>