Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 416
JMC Avatar asked Aug 18 '10 21:08

JMC


People also ask

What are encoding problems?

Once you have encoded something, you need to store it and be able to recall it. Problems with these last two stages are associated with conditions like dementia. But for most younger people, the problem lies in the encoding. Doing too many things at once means we're not able to give proper attention to any one task.

How do I fix encoding in Python?

The best way to attack the problem, as with many things in Python, is to be explicit. That means that every string that your code handles needs to be clearly treated as either Unicode or a byte sequence. The most systematic way to accomplish this is to make your code into a Unicode-only clean room.

How do I fix corrupted character encoding?

Go to "File" -> "Options" -> "Advanced" and scroll down until the "General" section is reached. In the "General" section, check the box that says "Confirm file format conversion on open." Exit Word, and reopen the corrupt document again. The dialogue box will appear.


2 Answers

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">
like image 172
Sergey Eremin Avatar answered Oct 06 '22 23:10

Sergey Eremin


Use htmlspecialchars_decode. Example straight from the PHP documentation page:

$str = '<p>this -&gt; &quot;</p>';
echo htmlspecialchars_decode($str); // <p>this -> "</p>
like image 24
mhitza Avatar answered Oct 06 '22 22:10

mhitza