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

<searchLink fieldCode="AR" term="%22Krugman%2C+Paul+R%2E%22">Krugman, Paul R.</searchLink>

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:

function removeLink($data){
	$data = htmlspecialchars_decode($data);
	$data = str_replace('><', '>; <', $data);
	return strip_tags($data);
}

 

PHP – removing unwanted XML from string
Scroll to top