开发者

Detect latin1 characters in utf8 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


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


Use htmlspecialchars(); it is more safer for work. More info:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜