简体   繁体   中英

Show spanish characters in a form

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!

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

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;`

You might be looking for htmlspecialchars.

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

outputs &lt;á&gt; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM