通过PHP从字符串中删除特定的XML标记


Remove specific XML tag from string via PHP

我只需要从保存到变量的XML文件中删除特定的标记</licenses>

我试过了,但没有得到预期的输出:

<?php
  print preg_replace("</licenses>", "", "</licenses>");
?>

退货:

<>

令人惊讶的是,以下内容删除了所有标签的内容:

<?php
  print preg_replace("<>", "", "</licenses>");
?>

我所能想到的是,我在某种程度上达到了正则表达式模式或其他什么。我该怎么做?

您需要在preg_replace的第一个参数中使用正则表达式分隔符,该参数是正则表达式:

 echo preg_replace("#</licenses>#", "", "</licenses>");

这将按预期返回一个空字符串。

您可以使用它。

print preg_replace("/<'/licenses>/", "", "</licenses>");