检测utf8字符串中的latin1字符

时间:2011-08-24 15:17:25

标签: php encoding utf-8 latin1

我从数据库获取utf8编码的数据。但不知何故,一些旧数据包含latin1字符。

所以这个

$encod = mb_detect_encoding($string, 'UTF-8', true);

始终是正确的。

总是使用utf8_decode()来检查像'äöüß'这样的拉丁文字符是否安全?

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

此致

2 个答案:

答案 0 :(得分:0)

似乎没有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

答案 1 :(得分:-2)

使用htmlspecialchars();工作更安全。 更多信息:

http://php.net/manual/en/function.htmlspecialchars.php