I am trying to replace accented characters with the normal replacements. Below is what I am currently doing.
$string = \"Éric Cantona\";
$strict = st
I know, that question has been asked a long long time ago...
I was looking for a short and elegant solution, but couldn't find satisfaction for two reasons:
First, most of the existing solutions replace a list of characters by a list of other characters. Unfortunately, it require to use a specific encoding for the php script file itself which might be unwanted.
Second, using iconv seems to be a good way, but it's not enough as the result of a converted character could be one or two characters, or a Fatal Exception.
So I wrote that small function which does the job :
function replaceAccent($string, $replacement = '_')
{
$alnumPattern = '/^[a-zA-Z0-9 ]+$/';
if (preg_match($alnumPattern, $string)) {
return $string;
}
$ret = array_map(
function ($chr) use ($alnumPattern, $replacement) {
if (preg_match($alnumPattern, $chr)) {
return $chr;
} else {
$chr = @iconv('ISO-8859-1', 'ASCII//TRANSLIT', $chr);
if (strlen($chr) == 1) {
return $chr;
} elseif (strlen($chr) > 1) {
$ret = '';
foreach (str_split($chr) as $char2) {
if (preg_match($alnumPattern, $char2)) {
$ret .= $char2;
}
}
return $ret;
} else {
// replace whatever iconv fail to convert by something else
return $replacement;
}
}
},
str_split($string)
);
return implode($ret);
}