65

For special characters like áéí, I can call htmlentities():

$mycaption = htmlentities($mycaption, ENT_QUOTES);

To get the corresponding html entities:

áéí

How can I reverse this back to áéí ?

Zuul
  • 16,217
  • 6
  • 61
  • 88
Uli
  • 2,625
  • 10
  • 46
  • 71

5 Answers5

109

If you use htmlentities() to encode, you can use html_entity_decode() to reverse the process:

html_entity_decode()

Convert all HTML entities to their applicable characters.

html_entity_decode() is the opposite of htmlentities() in that it converts all HTML entities in the string to their applicable characters.

e.g.

$myCaption = 'áéí';

//encode
$myCaptionEncoded = htmlentities($myCaption, ENT_QUOTES);

//reverse (decode)
$myCaptionDecoded = html_entity_decode($myCaptionEncoded);
Community
  • 1
  • 1
heximal
  • 10,327
  • 5
  • 46
  • 69
4

You want to look at html_entity_decode and worry about which charset you should be using (probably ISO8859-1).

It may also be worth reading this article about character sets etc.

ADW
  • 4,030
  • 17
  • 13
  • I tried this code but the characters stay encoded: `$strcaption = html_entity_decode($finalArray['Caption'], ENT_COMPAT, "ISO8859-1"); ` What's wrong with it? – Uli Jun 24 '11 at 09:17
  • The following works for me print html_entity_decode("á",ENT_COMPAT, "ISO8859-1"); you may want to check that $finalArray['Caption'] doesn't have things double quoted (e.g. ampersand characters represented as & etc.) – ADW Jun 24 '11 at 09:35
  • `you may want to check that $finalArray['Caption'] doesn't have things double quoted (e.g. ampersand characters represented as & etc.)` I think this could also be a problem. What should I do to fix that? – Uli Jun 24 '11 at 10:00
0
string html_entity_decode ( string $string [, int $quote_style = ENT_COMPAT [, string $charset = 'UTF-8' ]] )
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Eamorr
  • 9,872
  • 34
  • 125
  • 209
0

I think you are looking for html_entity_decode.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

html_entity_decode(). This can be found at the very beginning of the documentation for htmlentities

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • Does somebody know why this is working `html_entity_decode(" áé",ENT_COMPAT, "ISO8859-1")` but if the same text comes from a variable it's not working anymore: `html_entity_decode($mycaption,ENT_COMPAT, "ISO8859-1");` What's the difference? – Uli Jun 24 '11 at 13:44
  • What do you mean "not working anymore"? It's likely that $mycaption doesn't contain the same string as in your first example. They may look the same on your screen but have different encoding. – Jordan Running Jun 24 '11 at 18:28
  • It's getting closer. Problem: If I print the decoded it shows me the correct characters `$reversecaption = html_entity_decode($finalArray['Caption'],ENT_COMPAT, "ISO8859-1"); print_r(html_entity_decode($reversecaption,ENT_COMPAT, "ISO8859-1"));` but if I want to use it within the meta title it's the undecoded. `<?php echo $reversecaption; ?>` Does anybody have an idea why is that? – Uli Jun 24 '11 at 23:35
  • Uli: PHP doesn't know or care about your HTML. If you're using the same code in two places, it will output the same data in both places, regardless of the HTML tags. More likely is that your web browser is displaying them differently. – Jordan Running Jun 25 '11 at 23:42