PHP – removing unwanted XML from string

Yesterday I was working on a small PHP script in which an API sends “xml” within the response. That XML is supposed to make handling the data easier, but to be fair I just didn’t need it.

This is the snippet that the API returned

At first I tried to simply use PHP’s strip_tags() function, but I just wasn’t lucky, until I realized – much to my embarrassment – that I first needed to run the string through PHP’s htmlspecialchars_decode() function. Too me literarily a quarter hour to see the obvious.

As the string I was given would contain multiple authors and there was no delimiter, I also threw in a quick string replace that would add a semicolon in. The super tiny function thus looks like this:


You Might Also Like