While I was working on a project my colleague provided me a list of foreign characters which they needed to be replaced from string. I have made an usable JSON array. Save the following content in a file called ForeignCharactersList.json
{
"chars": [
{
"à": "a",
"è": "e",
"ì": "i",
"ò": "o",
"ù": "u",
"À": "A",
"È": "E",
"Ì": "I",
"Ò": "O",
"Ù": "U",
"á": "a",
"é": "e",
"í": "i",
"ó": "o",
"ú": "o",
"ý": "y",
"Á": "A",
"É": "E",
"Í": "I",
"Ó": "O",
"Ú": "U",
"Ý": "Y",
"â": "a",
"ê": "e",
"î": "i",
"ô": "o",
"û": "u",
"Â": "A",
"Ê": "E",
"Î": "I",
"Ô": "O",
"Û": "U",
"ä": "a",
"ë": "e",
"ï": "i",
"ö": "o",
"ü": "u",
"ÿ": "y",
"Ä": "A",
"Ë": "E",
"Ï": "I",
"Ö": "O",
"Ü": "U",
"Ÿ": "Y",
"å": "a",
"Å": "A",
"æ": "a",
"Æ": "A",
"ã": "a",
"ñ": "n",
"õ": "o",
"Ã": "A",
"Ñ": "N",
"Õ": "O",
"ç": "c",
"Ç": "C",
"ð": "d",
"Ð": "D",
"ø": "o",
"Ø": "O",
"œ": "o",
"Œ": "C",
"š": "s",
"Š": "S",
"ß": "B",
"¿": "?",
"¡": "!"
}
]
}
And then using a php class, define a method like below:
<?php
public function replaceForeignCharacters($string)
{
$allForeignCharacters = json_decode(
file_get_contents(__DIR__ . '/ForeignCharactersList.json'),
true
);
$allForeignCharacters = $allForeignCharacters['chars'][0];
$replacedString = strtr($string, $allForeignCharacters);
return $replacedString;
}
/* use case */
$someString = 'This string hås søme foreign çhåråcters';
$outputString = $this->replaceForeignCharacters($someString);
print_r($outputString);
I have used the following list in http://www.objgen.com/json to get the JSON array:
chars [] à = a è = e ì = i ò = o ù = u À = A È = E Ì = I Ò = O Ù = U á = a é = e í = i ó = o ú = o ý = y Á = A É = E Í = I Ó = O Ú = U Ý = Y â = a ê = e î = i ô = o û = u  = A Ê = E Î = I Ô = O Û = U ä = a ë = e ï = i ö = o ü = u ÿ = y Ä = A Ë = E Ï = I Ö = O Ü = U Ÿ = Y å = a Å = A æ = a Æ = A ã = a ñ = n õ = o à = A Ñ = N Õ = O ç = c Ç = C ð = d Ð = D ø = o Ø = O œ = o Œ = C š = s Š = S ß = B ¿ = ? ¡ = !
Cheers!