Conversion of special characters in PHP

2019-07-06 11:54发布

I've tried many functions already, but I simply can't figure this out. The right way, anyway.

In a form field called description, I can expect all kinds of characters. These need to be formatted into HTML entities before they're submitted to the db.

Now, my code:

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

For a MySQL query, I simply add a "safe" function to slash the ' off the string:

mysql_real_escape_string($formdesc);

However, this sometimes doesn't work. "é," for instance, becomes é instead of é.

There must be a normal function for this. Does anyone know what I mean?

3条回答
ら.Afraid
2楼-- · 2019-07-06 12:14

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.

查看更多
forever°为你锁心
3楼-- · 2019-07-06 12:16

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.

查看更多
Luminary・发光体
4楼-- · 2019-07-06 12:17

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

Josh

查看更多
登录 后发表回答