Convert 2 similarly-looking German characters of different kinds to same ASCII string in PHP

前端 未结 3 982
梦如初夏
梦如初夏 2021-01-07 00:24

I have these two strings:

$str1 = \'Ö\';
$str2 = \'Ö\';
$e1 = mb_detect_encoding($str1);
$e2 = mb_detect_encoding($str2);
var_dump($str1);
var_dump($str2);
         


        
3条回答
  •  遥遥无期
    2021-01-07 01:11

    Extending Andreas's answer. These characters are letter + combining diaeresis(U-0308). I was able to search and replace them to standard umlauts, then replace with whatever is needed. This is the fuction I've used to replace them:

    function convertToUmlauts($str) {
        $srp_array = ['Ö' => 'Ö', 'Ä' => 'Ä', 'Ü' => 'Ü', '̈a' => 'ä', 'ö' => 'ö', 'ü' => 'ü'];
        return strtr($str, $srp_array);
    }
    

提交回复
热议问题