是否有任何内置的PHP函数来取代德语"Umlaute"?



我必须在php代码中替换德语"Umlaute"的Html-represantation我是这样做的:

Private function replaceHTMLEntities(&$str){
$str = str_replace('Ä',chr(196),$str); 
    $str = str_replace('Ö',chr(214),$str);
    $str = str_replace('Ü',chr(220),$str); 
    $str = str_replace('ä',chr(228),$str);
    $str = str_replace('ö',chr(246),$str);
    $str = str_replace('ü',chr(252),$str);
    $str = str_replace('ß',chr(223),$str);
}

php 中是否有任何内置函数来缩短此代码?

我不确定内置函数,但至少您可以使用str_replace参数作为数组来减少和优化代码:

private function replaceHTMLEntities(&$str){
    $search  = ['Ä', 'Ö', 'Ü']; // and others...
    $replace = [chr(196), chr(214), chr(220)]; // and others...
    $str = str_replace($search, $replace, $str);
}

提示:如果可能,不要使用按引用传递。调试更难,更改也不明显。

迟到总比没有好。这对我有用。

$inputString2 = "Schöner Graben Straße. Gülpät Älbeg Ürh Örder ";
function replaceHTMLEntities($str)
{
    $str = str_replace('Ä', 'Ae', $str);
    $str = str_replace('ä', 'ae', $str);
    $str = str_replace('Ö', 'Oe', $str);
    $str = str_replace('ö', 'oe', $str);
    $str = str_replace('Ü', 'Ue', $str);
    $str = str_replace('ü', 'ue', $str);
    $str = str_replace('ß', 'ss', $str);
    return $str;
}
echo replaceHTMLEntities($inputString2);

最新更新