Eregi_replace for preg_replace

Asked

Viewed 61 times

0

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

"Caio didn’t take a bucket of water" for "caio-não-pegou-um-balde-Dagua" and then would be: com.br/caio-nao-pegou-um-balde-Dagua

Currently use eregi_replace and with PHP 7 no longer serves me, and then need a new way to run 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);
}
  • Gabriel recommending the video on youtube https://www.youtube.com/watch?v=iPiTB4ho5DA =D. Here’s what you want http://thiagomorello.com/blog/2013/01/howto perfectly generate urls-friends-com-php/.

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");

https://stackoverflow.com/questions/4783802/converting-string-into-web-safe-uri

Browser other questions tagged

You are not signed in. Login or sign up in order to post.