Preg_replace for Preg_replace_callback

1

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.

    
asked by anonymous 29.09.2015 / 14:36

1 answer

0

First of all I recommend reading from this question because the answers and by the related article you will see that REGEX in PHP does not support Unicode characters, as well as said by @mgibsonbr.

In this way the expressions

'/[áaãâäÁAAÂÄ]/'
'/[éèêë&ÉEeË]/'
'/[íìîïÍ]/'
'/[óòõôöOÔÓÖO]/'
'/[úùûüÚUUÜ]/'
'/[çÇ]/'
'/[ñnN]/'

will not work.

To work around this problem you can use the str_replace function, which will search exactly for the specified character.

For example this function:

function changeLetters($string, $down = true){

    $letters = array(
        'A'=>array('@','â','ä','à','å','Ä','Å','á','ª','Á','Â','À','ã','Ã'),
        'E'=>array('&','é','ê','ë','è','É','£','Ê','Ë','È'),
        'I'=>array('!','ï','î','ì','¡','Í','Î','Ï','Ì','í'),
        'O'=>array('ô','ö','ò','Ö','ø','Ø','ó','º','¤','ð','Ó','Ô','Ò','õ','Õ'),
        'U'=>array('ü','û','ù','Ü','ú','µ','Ú','Û','Ù'),
        'B'=>array('ß'),
        'C'=>array('Ç','ç','©','¢'),
        'D'=>array('Ð'),
        'F'=>array('ƒ'),
        'L'=>array('¦'),
        'N'=>array('ñ','Ñ'),
        'S'=>array('$','§'),
        'X'=>array('×'),
        'Y'=>array('ÿ','¥','ý','Ý'),
        'AE'=>array('æ','Æ'),
        'P'=>array('þ','Þ'),
        'R'=>array('®'),
        '0'=>array('°'),
        '1'=>array('¹','ı'),
        '2'=>array('²'),
        '3'=>array('³'),
    );

    foreach ($letters as $letter => $change){
        if($down){ $letter = down($letter); }
        $string = str_replace($change, $letter, $string);
    }

    return $string;
}
    
29.09.2015 / 15:06