2

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?

1
  • It is because < and > are used as the delimiters. Only what is inside them, "/licenses", will be replaced. Commented Dec 17, 2013 at 12:32

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use it.

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

1 Comment

< and > are not special characters and do not need to be escaped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.