I would like to build a normalization functions for strings:
'abcd 0123' -> 'abcd0123'
'Déjà vue' -> 'Dejavue' //some specific characters are converted
'&§%-chars' -> 'chars' //unrecognized characters are dropped
I used following code:
function normalize(string $text): string {
$patterns = array('/[éèë]/i', '/à/','/ç/','/[^a-z0-9]/i');
$replacements = array('e','a','c');
return preg_replace($patterns, $replacements, $text);
}
The last pattern has no replacement, so it should replace with an empty string (wanted).
However, this is nog having the desirable effect:
'abcd 0123' -> 'abcd0123' //ok
'Déjà vue' -> 'Deejevue' //want 'Dejavue'
'&§%-chars' -> 'chars' //ok
How can I correct my code to get the desired result?