检测utf8字符串中的latin1字符
我从数据库中获取 utf8 编码的数据。但不知何故,一些旧数据包含 latin1 字符。
所以这
$encod = mb_detect_encoding($string, 'UTF-8', true);
始终是正确的。
始终使用 utf8_decode() 来检查像“äöüß”这样的 latin1 字符是否安全?
$string = utf8_decode($string);
$search = Array(" ", "ä", "ö", "ü", "ß", "."); //,"/Ä/","/Ö/","/Ü/");
$replace = Array("-", "ae", "oe", "ue", "ss", "-"); //,"Ae","Oe","Ue");
$string = str_replace($search, $replace, strtolower($string));
问候
I get data from the database that is utf8 encoded. But somehow some old data contains latin1 characters.
So this
$encod = mb_detect_encoding($string, 'UTF-8', true);
always is correct.
Is it safe to always use utf8_decode() to check for latin1 characters like 'äöüß'???
$string = utf8_decode($string);
$search = Array(" ", "ä", "ö", "ü", "ß", "."); //,"/Ä/","/Ö/","/Ü/");
$replace = Array("-", "ae", "oe", "ue", "ss", "-"); //,"Ae","Oe","Ue");
$string = str_replace($search, $replace, strtolower($string));
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它似乎可以在没有
utf8_encoding
的情况下工作:演示:http://codepad.org/HGTyHkBU
It seems to work without the
utf8_encoding
:DEMO: http://codepad.org/HGTyHkBU
使用 htmlspecialchars();工作更安全。
更多信息:
http://php.net/manual/en/function.htmlspecialchars.php< /a>
Use htmlspecialchars(); it is more safer for work.
More info:
http://php.net/manual/en/function.htmlspecialchars.php