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

I need to remove only the specific tag </licenses> from an XML file saved to a variable.

I've tried this, but I'm not getting the expected output:

<?php

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

?>

Returns:

<>

And surprisingly, the following removes the contents of all tags:

<?php

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

?>

All I can think is that I'm somehow hitting a regex pattern or something. How can I do this?

You need to use regex delimiter in the first argument of preg_replace which is a regex:

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

This will return an empty string as expected.

You can use it.

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