1

I have to replace Html-represantation of German "Umlaute" in php-code I do this like this:

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

Is there any inbuild-function in php to shorten this code?

brombeer
  • 8,716
  • 5
  • 21
  • 27
Max Lindner
  • 171
  • 1
  • 1
  • 7
  • 1
    https://www.php.net/manual/en/function.html-entity-decode.php ? – Ugur Eren Apr 08 '19 at 11:40
  • [Replacing German chars with Umlaute to simple latin chars php](https://stackoverflow.com/q/41475937/2943403) and [Replacing accented characters php](https://stackoverflow.com/q/3371697/2943403) – mickmackusa Mar 17 '23 at 08:10

2 Answers2

1

I'm not sure about build-in function for this but at least you can reduce and optimize you code using str_replace with parameters as arrays:

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

    $str = str_replace($search, $replace, $str);
}

Hint: do not use passing by reference if it is possible. It's harder to debug and changes are not obvious.

Slava
  • 878
  • 5
  • 8
0

Better Late than never. This what worked for me.

$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);
Isaac Hatilima
  • 151
  • 1
  • 10