检测utf8字符串中的latin1字符

发布于 2024-11-30 23:40:33 字数 488 浏览 0 评论 0原文

我从数据库中获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花想c 2024-12-07 23:40:33

它似乎可以在没有 utf8_encoding 的情况下工作:

<?php
   $string = "äöüß";
   $search = Array(" ", "ä", "ö", "ü", "ß", "."); //,"/Ä/","/Ö/","/Ü/");
   $replace = Array("-", "ae", "oe", "ue", "ss", "-"); //,"Ae","Oe","Ue");
   $string = str_replace($search, $replace, strtolower($string));
   echo $string;
?>

演示:http://codepad.org/HGTyHkBU

It seems to work without the utf8_encoding:

<?php
   $string = "äöüß";
   $search = Array(" ", "ä", "ö", "ü", "ß", "."); //,"/Ä/","/Ö/","/Ü/");
   $replace = Array("-", "ae", "oe", "ue", "ss", "-"); //,"Ae","Oe","Ue");
   $string = str_replace($search, $replace, strtolower($string));
   echo $string;
?>

DEMO: http://codepad.org/HGTyHkBU

追我者格杀勿论 2024-12-07 23:40:33

使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文