24

The following code outputs an empty string. The cause is the "ó" in $text, but why? What characters does utf-8 encode then?

The problem is solved when using iso-8859-1, but I need to use utf-8, so what am I doing wrong?

<!doctype html>
<head>
  <meta charset="utf-8">
</head>

<body>
<?
$text = 'Hola ó Hola';
$text = htmlentities($text,ENT_QUOTES,'utf-8');
echo $text;
?>
</body>
</html>
2
  • Strange, your code seems to work fine on my computer. (returns &oacute;) - the only thing I changed was changing <? to <?php, you could try that. Commented Dec 9, 2011 at 2:39
  • I Think i may be saving my files in the wrong charset ? Is that possible ? Commented Dec 9, 2011 at 3:15

4 Answers 4

39

I had a similar issue and used the flag ENT_SUBSTITUTE to prevent the empty string. It still didn't encode, and I couldn't rely on the file being UTF-8, so I converted the encoding on just the string:

$text = htmlentities(mb_convert_encoding($text, 'UTF-8', 'ASCII'), ENT_SUBSTITUTE, "UTF-8");
Sign up to request clarification or add additional context in comments.

2 Comments

This is the option that worked for me as far as the string not being blank, but it is not showing the special characters, those are just omitted from the string. Any ideas on how to get the special characters to show? In my case, it is a bullet, read in from an old db.
$text = htmlentities(utf8_encode($text)); also works.
8

Make sure you save your source file as UTf-8 if it contains the string. Else make sure that whatever is supplying the string supplies it as UTF-8.

10 Comments

What if the source of the text is a $_POST ? Where do i get the char set encoding?
If the POST request comes from a form on the same website which emits a utf-8 meta-charset you can assume the input charset will be utf-8 as well (your browser should adhere to whatever charset header is emitted, but it might chose not to at the users discretion). I suppose you could try some PHP functions to see what PHP thinks the charset is; like iconv_get_encoding but I've used it with mixed success.
K thanks. Is there any resource/guide that could help me understand charsets better ? Im just confused with so many charsets: The charset on <meta charset="">, the charset on MySQL collation, the charset on which the file is saved. It messed my head up :P
It's very simple really. Always use utf-8 for everything, always. Source files, MySQL NAMES the who shebang. But yea, you do have to always specific that you want utf-8 because for some bizarre reason it's not the default.
The problem is, as far as i can tell, i AM using utf-8 for everything, but something's not getting encoded ! How can i contact u personally so i can explain my error please ? :)
|
0
  1. Open your code editor (notepad++ for instance or other).
  2. Click in New > Save As.. put the name of file (blank for a while) and in type select PHP Hypertext ...
  3. Now copy all content of your original file and put in this new file.
  4. Click in save and try.

Comments

0

Just commenting for others who may have a similar problem as myself. $var containing special characters. This …

<?= htmlentities($var) ?>

… gave me an empty output, while …

<?php echo htmlentities($var); ?>

… worked fine. Same with htmlspecialchars. Never encountered this before because I normally don't use it that way but here I was just testing something out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.