How to replace special characters with accents within multi dimensional associative arrays to normalized/latin ones in php?

StackOverflow https://stackoverflow.com/questions/20725811

Domanda

This is a follow up question from this one: How to remove spaces in array keys names in php?

Basically this function works perfectly:

function fixArrayKey(&$arr)
{
    $arr=array_combine(array_map(function($str){return str_replace(array(" ",",",".","-","+"),"",$str);},array_keys($arr)),array_values($arr));
    foreach($arr as $key=>$val)
    {
        if(is_array($val)) fixArrayKey($arr[$key]);
    }
}

But when I add all the special characters I want to replace, it still works but does not replace/remove any of the special/accented charaters:

function fixArrayKey(&$arr)
{
    $arr=array_combine(array_map(function($str){return str_replace(array(" ",",",".","-","+",   "á",    "Á",    "à",    "À",    "ă",    "Ă",    "â",    "Â",    "å",    "Å",    "ã",    "Ã",    "ą",    "Ą",    "ā",    "Ā",    "ä",    "Ä",    "æ",    "Æ",    "ḃ",    "Ḃ",    "ć",    "Ć",    "ĉ",    "Ĉ",    "č",    "Č",    "ċ",    "Ċ",    "ç",    "Ç",    "ď",    "Ď",    "ḋ",    "Ḋ",    "đ",    "Đ",    "ð",    "Ð",    "é",    "É",    "è",    "È",    "ĕ",    "Ĕ",    "ê",    "Ê",    "ě",    "Ě",    "ë",    "Ë",    "ė",    "Ė",    "ę",    "Ę",    "ē",    "Ē",    "ḟ",    "Ḟ",    "ƒ",    "Ƒ",    "ğ",    "Ğ",    "ĝ",    "Ĝ",    "ġ",    "Ġ",    "ģ",    "Ģ",    "ĥ",    "Ĥ",    "ħ",    "Ħ",    "í",    "Í",    "ì",    "Ì",    "î",    "Î",    "ï",    "Ï",    "ĩ",    "Ĩ",    "į",    "Į",    "ī",    "Ī",    "ĵ",    "Ĵ",    "ķ",    "Ķ",    "ĺ",    "Ĺ",    "ľ",    "Ľ",    "ļ",    "Ļ",    "ł",    "Ł",    "ṁ",    "Ṁ",    "ń",    "Ń",    "ň",    "Ň",    "ñ",    "Ñ",    "ņ",    "Ņ",    "ó",    "Ó",    "ò",    "Ò",    "ô",    "Ô",    "ő",    "Ő",    "õ",    "Õ",    "ø",    "Ø",    "ō",    "Ō",    "ơ",    "Ơ",    "ö",    "Ö",    "ṗ",    "Ṗ",    "ŕ",    "Ŕ",    "ř",    "Ř",    "ŗ",    "Ŗ",    "ś",    "Ś",    "ŝ",    "Ŝ",    "š",    "Š",    "ṡ",    "Ṡ",    "ş",    "Ş",    "ș",    "Ș",    "ß",    "ť",    "Ť",    "ṫ",    "Ṫ",    "ţ",    "Ţ",    "ț",    "Ț",    "ŧ",    "Ŧ",    "ú",    "Ú",    "ù",    "Ù",    "ŭ",    "Ŭ",    "û",    "Û",    "ů",    "Ů",    "ű",    "Ű",    "ũ",    "Ũ",    "ų",    "Ų",    "ū",    "Ū",    "ư",    "Ư",    "ü",    "Ü",    "ẃ",    "Ẃ",    "ẁ",    "Ẁ",    "ŵ",    "Ŵ",    "ẅ",    "Ẅ",    "ý",    "Ý",    "ỳ",    "Ỳ",    "ŷ",    "Ŷ",    "ÿ",    "Ÿ",    "ź",    "Ź",    "ž",    "Ž",    "ż",    "Ż",    "þ",    "Þ",    "µ",    "а",    "А",    "б",    "Б",    "в",    "В",    "г",    "Г",    "д",    "Д",    "е",    "Е",    "ё",    "Ё",    "ж",    "Ж",    "з",    "З",    "и",    "И",    "й",    "Й",    "к",    "К",    "л",    "Л",    "м",    "М",    "н",    "Н",    "о",    "О",    "п",    "П",    "р",    "Р",    "с",    "С",    "т",    "Т",    "у",    "У",    "ф",    "Ф",    "х",    "Х",    "ц",    "Ц",    "ч",    "Ч",    "ш",    "Ш",    "щ",    "Щ",    "ъ",    "Ъ",    "ы",    "Ы",    "ь",    "Ь",    "э",    "Э",    "ю",    "Ю",    "я",    "Я"),"",$str);},array_keys($arr)),array_values($arr));
    foreach($arr as $key=>$val)
    {
        if(is_array($val)) fixArrayKey($arr[$key]);
    }
}

Does anyone know why ? And how to solve my problem?

È stato utile?

Soluzione

Depending on what you need, at least one of the following should work:
1) preg_replace("/[\\x{20}\\x{2B}-\\x{2E}\\x{80}-\\x{FFFF}]+/u", "", $str)
2) preg_replace("/[^a-zA-Z0-9]+/u", "", $str)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top