I'm having trouble migrating a script of mine that clears characters from a sentence.
The preg_replace
(deprecated) I use the value and key of the array for the swap, however the preg_replace_callback
converts what it finds in Regex to another array independent, prevented that I use the previous key.
function LimpaTexto($texto) {
$texto = html_entity_decode($texto);
$texto = strtolower(trim($texto));
$replaces = array(
'/[áaãâäÁAAÂÄ]/' => 'a',
'/[éèêë&ÉEeË]/' => 'e',
'/[íìîïÍ]/' => 'i',
'/[óòõôöOÔÓÖO]/' => 'o',
'/[úùûüÚUUÜ]/' => 'u',
'/[çÇ]/' => 'c',
'/[ñnN]/' => 'n',
'/\s[\s]+/' => '-',
'/( )/' => '-',
'/( )\/( )/' => '-',
'/( )[-]( )/' => '-',
'/\//' => '-',
'/[^a-z0-9\-_]/' => '',
'/-+/' => '-',
'/[.]/' => '-'
);
$texto = preg_replace(array_keys($replaces), array_values($replaces), $texto);
return $texto;
}
How do you see array_keys
is found and switches to array_values
.
I could not get a formula to use in preg_replace_callback
. Only if I break character by character into the callback function and compare them to change, which makes script more costly in performance and size.