2010-11-08 19 views
3

嘿,這是我在stackoverflow上的第一篇文章。在PHP中用e替換é

我試圖用e和其他類似的特殊字符替換é。我試過str_replace()並將其從UTF-8轉換爲ASCII,但似乎沒有任何工作。當我將它從UTF-8轉換爲任何東西時,它只會將關閉。當我使用str_replace()時,它永遠不會捕獲它,而é仍然存在。

我有一種感覺,我們的服務器在內部有些問題,因爲我的朋友在他的服務器上嘗試了str_replace(),它工作正常。

感謝,

傑森Tolhurst

+0

可能的重複[需要將PHP轉換爲e](http://stackoverflow.com/questions/4125658/need-to-convert-e-to-e-in-php) – 2010-11-08 17:12:43

回答

7
$string = iconv('utf-8','ASCII//IGNORE//TRANSLIT',$string); 
+0

是的,結束。 – 2010-11-08 17:08:19

+0

它不在這裏.....你可以到達命令行,看看兩個字符集是否都受'iconv -l'支持?而'mb_detect_encoding($ string);'說的是字符串的可能字符集(它可能不是utf-8)? – Wrikken 2010-11-08 17:09:43

+0

我可以到那裏如何檢查它? – 2010-11-08 17:11:13

4

您可以使用htmlentities()é轉換爲é,然後使用正則表達式的符號後拉出第一個字符。

function RemoveAccents($string) { 
    // From http://theserverpages.com/php/manual/en/function.str-replace.php 
    $string = htmlentities($string); 
    return preg_replace("/&([a-z])[a-z]+;/i", "$1", $string); 
} 
+1

Yay代表'€',''+,'&','','&'和[其他人的加載](http://www.w3.org/TR/WD-html40-970708/sgml/entities.html ),恕我直言,你正在引入新的問題,雖然原來的問題確實解決了。 – Wrikken 2010-11-08 17:15:26

+0

你說得很對,雖然這可能會或可能不會成爲問題,這取決於用例。 – TRiG 2010-11-08 17:16:58

+0

'$ string = preg_replace(array('/ ß /','/&(..)lig;/','/&([aeioucyAEIOUCY])(uml | ring | cedil | circ | grave |斜線); /','/ &(.)[^;] *; /'),array('ss',「$ 1」,「$ 1」,「」),$ string);' – Aistina 2010-11-08 17:30:16