2

I have recently had some visitors with the following useragent:

Mozilla/5.0 (Linux; U; Android 2.3.6; es-co; XT320 Build/GRK39F) 
AppleWebKit/533.1 (KHTML, like Gecko) Versión/4.0 Mobile Safari/533.1

I insert these in a mysql table on a column that is utf8_general_ci . My PHP site is also served as UTF-8. Unfortunately, I have gotten some errors from these visitors as follows:

Incorrect string value: '\xF3n/4.0...' for column 'useragent' at row 1 [1366]

Hence it is the ó that is causing the problem. In my quest to resolve this, I changed the useragent of my firefox browser to this as well (with UA switcher plugin), but this gets inserted perfectly into my database.

So, my question: how is it possible that in some cases it's apparently not working? And how could this be resolved? I could of course just replace ó with o, but I would think this will not be the last special char I'll be encountering in the future...

4

2 回答 2

1

您的数据库可能希望发送 UTF-8 编码的数据,但用户代理莫名其妙地以 Latin-1 或其他一些特殊编码进行编码。这不应该,因为 HTTP 标头不应该使用非 ASCII 字符,因为没有指定 HTTP 标头如何编码的机制,因此不可能知道一个正在处理的编码是什么。

您发现了一个不合格的用户代理。您应该检查所有收到的字符串是否符合您的预期编码;例如使用mb_check_encoding($str, 'UTF-8'). 如果字符串不是预期的编码,所有的赌注都被取消,你可以做任何你认为正确的事情。丢弃字符串,尝试猜测它的编码并转换它,替换无效字节或其他任何你喜欢的东西。

于 2013-07-09T19:39:21.050 回答
0

可能是该字符串是使用用户覆盖的代码页生成的。你可以试试

$Str =  mb_convert_encoding ( string , "auto", "UTF-8")  

在将其发送到数据库之前

于 2013-07-09T19:50:17.683 回答