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));

问候

4

2 回答 2

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;
?>

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

于 2011-08-24T15:28:03.580 回答
-2

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

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

于 2011-08-24T15:27:47.247 回答