0

I have a form and in a textarea I want to display some text that have some spanish characters but encoded as html. The problem is that instead of the spanish character it displays the html code. I'm using htmlentities to display it in the form. my code to display is:

<?php echo htmlentities($string, ENT_QUOTES, "UTF-8") ?>

Any idea or I just shouldnt use htmlentities in a form? Thanks!

EDIT Lets say $string = 'á'

When I just do <?php echo $string ;?> I get á If I do <?php echo htmlentities($string, ENT_QUOTES, "UTF-8") ?> I get &aacute;

I'm so confused!

raygo
  • 1,348
  • 5
  • 18
  • 40
  • Do you get your desired result if you replace htmlentities with [htmspecialchars](http://php.net/manual/de/function.htmlspecialchars.php) ? – pce Nov 08 '12 at 15:02

3 Answers3

0

If I understand you correctly, you need to use...

<meta charset="utf-8">

in your page header, and then...

<?php echo html_entity_decode($string, ENT_QUOTES); ?>

This will convert your HTML entities back to their proper characters

0

You can try explicitly adding content type at the top of your file as below

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

if it's already encoded as html then you need to decode it now..you can use html_entity_decode($string);

Your string to be echoed in the form should be &aacute; as returned from database and not á

$string = '&aacute;'; // your string as fetched from database
echo html_entity_decode($string);// this will display á in the textarea

and before saving to database you need to

htmlentities($_POST['txtAreaName'], ENT_QUOTES, "UTF-8"); // return `&aacute;`
000
  • 3,976
  • 4
  • 25
  • 39
0

You might be looking for htmlspecialchars.

echo htmlspecialchars('<á>', ENT_COMPAT | ENT_HTML5, "UTF-8");

outputs &lt;á&gt;.

mabako
  • 1,213
  • 11
  • 19
  • Yes, right now it displays the html code, but its not displaying the user friendly one. Meaning, for á I get á . I would like it to display á but in the html for it to be á – raygo Nov 08 '12 at 19:28
  • Are you sure you aren't converting your & later again? – mabako Nov 08 '12 at 19:32
  • im sure, the html is only showing á, it would show &aacute; – raygo Nov 08 '12 at 19:44
  • `ENT_HTML5` is redundant with htmlspecialchars, see http://stackoverflow.com/a/14532168/427545 – Lekensteyn Jan 26 '13 at 00:28