2011-08-24 82 views
0

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

問候

+1

怎麼樣'mb_detect_encoding($字符串,「ISO-8859-1,UTF -8',true);'? – ajreal

+0

@ajreal我從數據庫中得到的字符串是'äääää'''code' mb_detect_encoding($ this - > _ name,'ISO-8859-1,UTF-8',true);'代碼'說'ISO-8859-1'和utf8_decode($ string)給出'ääää',我該怎麼辦? – spankmaster79

+1

嘗試搜索iconv,MB轉換通過PHP手冊/ SO,應該有幾個問題在這裏討論之前 – ajreal

回答

-1

似乎不上班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

不知道'-1'是什麼意思,但感謝^ _^ – Neal

+0

不是從我;-),但也不是一個很好的答案,因爲你放入$ string的字符串取決於你保存的文件的字符編碼代碼。我的數據來自datbase並且是utf8編碼,但包含latin1字符,如'äääää',它是utf8_decoded =äääää – spankmaster79