views:

678

answers:

3
+7  A: 

You need to specify the encoding for the htmlentities function (here UTF-8):

$formdesc = htmlentities($_POST['formdesc'], ENT_QUOTES, 'UTF-8');

Otherwise the default value ISO-8859-1 is used and the character é in your example encoded in UTF-8 as 0xC3A9 would be interpreted as two characters (Ã and ©).

But why do you use htmlentities anyway? If you just want to escape the HTML special characters like &, <, >, " and ' htmlspecialchars will suffice.

Gumbo
it works!! duh, so simple... but thx! btw > why > it seemed safer that way, am i wrong?
Maurice Kroon
You just need to replace additional character with character references if that characters cannot be encoded with the encoding you use. But as Unicode contains any known character and UTF-8 can encode it, you don’t need to replace any other characters than HTML’s special characters.
Gumbo
+1  A: 

have you tried looking at htmlspecialchars() and htmlspecialchars_decode()

Josh

Josh
Maurice Kroon
+1  A: 

Seems like the usual PHP escaping functions do not work on utf-8 text. Maybe Handling UTF-8 in JavaScript, PHP, and Non-UTF8 Databases will help you. Another source about utf-8 and PHP is the PHP UTF-8 cheatsheet.

Yuval F
thx for your comment but i've got it working now. i forgot the utf-8 tag yes, thx!
Maurice Kroon
You are welcome. I see Gumbo's answer was exactly the right thing to make this work.
Yuval F