0

I'm writing a simple webpage with PHP and for security reasons I pass certain form values through the PHP htmlentities() function. However, even though I specify a UTF-8 format it doesn't seem to be able to handle the characters "åäö". This is my code:

echo htmlentities("Hello", ENT_QUOTES, "UTF-8");
echo htmlentities("Hello åäö", ENT_QUOTES, "UTF-8");

The first line outputs "Hello" and the second one outputs "". Why is this? Have I missed anything?

EDIT:

There seems to be a problem with my encoding settings. This is the full code. Propably a silly error but I'm new to HTML/PHP.

<?php
header('Content-Type: text/html; charset=utf-8');
?>

<html>
    <head>
        <title>Testing</title>
    </head>
    <body>
        <?php echo htmlentities("Hello", ENT_QUOTES, "UTF-8") . htmlentities("Hello åäö", ENT_QUOTES, "UTF-8");?>
    </body>
</html>
Myone
  • 1,053
  • 2
  • 10
  • 24
  • 4
    http://3v4l.org/qe8CW looks fine when I test it there. Are your .php pages UTF-8? is your content-type utf-8? – Daan Jun 05 '14 at 12:37
  • This works for me as well. Make sure you are using the latest version of PHP and update your Operating System. – CheckeredMichael Jun 05 '14 at 12:39
  • 1
    use `header('Content-Type: text/html; charset=utf-8');` to force utf8 char encoding if it's not already by default. – Novocaine Jun 05 '14 at 12:40
  • To make sure that your page is stored UTF-8 encoded and has the proper header set, you can check your page with this [W3C checker](http://validator.w3.org/i18n-checker/). – martinstoeckli Jun 05 '14 at 12:43
  • Okay, that seems like something that I've missed. I have now edited my post with the full code. – Myone Jun 05 '14 at 14:19
  • Tell your editor to save files in UTF-8. – deceze Jun 05 '14 at 14:22
  • I wrote a small article that should get you started with [UTF-8 and PHP](http://www.martinstoeckli.ch/php/php.html#utf8). – martinstoeckli Jun 05 '14 at 15:08

0 Answers0