Eregi_replace for preg_replace [duplicate]

0

Regular expression is not my strong in PHP and I need your help. I need to transform a string that comes from a post apt to be part of a url. Example:

"Caius did not take a bucket of water" to "fell-did-not-catch-a-bucket" and then he would:

I currently use eregi_replace and PHP 7 does not suit me anymore, so I need a new way to do the same function.

Code:

public static function Url($texto){
    $texto = html_entity_decode($texto);
    $texto = @eregi_replace('[aáàãâä]','a',$texto);
    $texto = @eregi_replace('[eéèêë]','e',$texto);
    $texto = @eregi_replace('[iíìîï]','i',$texto);
    $texto = @eregi_replace('[oóòõôö]','o',$texto);
    $texto = @eregi_replace('[uúùûü]','u',$texto);
    $texto = @eregi_replace('[ç]','c',$texto);
    $texto = @eregi_replace('[ñ]','n',$texto);
    $texto = @eregi_replace('( )','-',$texto);
    $texto = @eregi_replace('[^a-z0-9\-]','',$texto);
    $texto = @eregi_replace('--','-',$texto);
    return strtolower($texto);
}
    
asked by anonymous 15.01.2018 / 05:16

1 answer

0

setlocale(LC_ALL, 'pt_BR.UTF8');
function toAscii($str, $replace=array(), $delimiter='-') {
    if( !empty($replace) ) {
    $str = str_replace((array)$replace, ' ', $str);
    }

    $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
    $clean = strtolower(trim($clean, '-'));
    $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

    return $clean;
}

echo toAscii("Caio não pegou um balde d'água para caio-nao-pegou-um-balde-dagua");

link

    
15.01.2018 / 05:40