Encoding issue, coverting & to & for html using php

JMC picture JMC · Aug 18, 2010 · Viewed 37.9k times · Source

I have a url in html:

<a href="index.php?q=event&amp;id=56&amp;date=128">

I need to turn it into a string exactly as:

<a href="index.php?q=event&id=56&date=128">

I know how to do this with preg_replace etc, but is there a function in php that deals directly with encoding that I can use for other encoding issues such as &nsbp (or whatever it is, etc)? Ideally I would send my string into the function and it would output '&' instead of &amp. Is there a universal function for converting &TEXT into an actual character?

Edit: sorry, posted this before I finished typing the question. QUESTION is now complete.

Answer

Sergey Eremin picture Sergey Eremin · Aug 18, 2010

use html_entity_decode():

$newUrl = html_entity_decode('<a href="index.php?q=event&amp;id=56&amp;date=128">');
echo $newUrl; // prints <a href="index.php?q=event&id=56&date=128">