开发者

Strange characters appearing after copy/pasting in forms/emails

开发者 https://www.devze.com 2023-02-07 09:23 出处:网络
I have users who sometimes paste things into my site\'s forms after copying something from their Gmail.The characters look normal when they paste it, but in the database they have extra special charac

I have users who sometimes paste things into my site's forms after copying something from their Gmail. The characters look normal when they paste it, but in the database they have extra special characters that appear.

Here is an example of the text with the the special characters. It originally happened on this page: http://www.hikingsanfrancisco.com/hiker_community/scheduled_hike_event.php?hike_event_id=91

But it looks like the person who made it has cleaned up the strange characters.

Does anyone know how to stop this from happening in the future?开发者_如何学C

Thanks, Alex

I use PHP and MySQL


I'd guess that you're getting UTF-8 encoded text but your database is configured for ISO-8859-1 (AKA Latin-1). The page you reference says:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

so it is claiming to be encoded as UTF-8. A form on a UTF-8 page will be sent back to the server in UTF-8. Then you send that UTF-8 data into your database where it is stored as Latin-1 encoded text. If you're not handling the UTF-8 to Latin-1 change yourself then you'll get "funny" characters when you send the data back to a browser. As long as the text only uses standard ASCII characters then everything will be okay as UTF-8 and Latin-1 overlap on the ASCII characters.

The solution is to pick a character encoding and use it everywhere. I'd recommend UTF-8 everywhere. However, if your database is already in Latin-1 then you'll have to go with Latin-1 or change the encoding in the database and re-encode all the data. But, if all the text in your database is simple ASCII then no re-encoding will be needed.

Hard to say what's going without examples but a character encoding mismatch is the usual problem when funny (funny peculiar, not funny ha-ha) characters appear only when text is sent back to the browser.

0

精彩评论

暂无评论...
验证码 换一张
取 消