Search code examples
phpcharacter-encodinghtml-entities

Encoding issue, coverting & to & for html using php


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.


Solution

  • 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">