Problem with function that generates friendly url

Asked

Viewed 534 times

0

Good

I have a function that generates the name for the friendly urls but I’m having a problem that when the function generates the name if that name contains accents it does not put the same word but without accent puts another character

Function

function url_amigavel($string) {
        $palavra = strtr($string, "ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ", "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");
        $palavranova = str_replace("_", " ", $palavra);
        $pattern = '|[^a-zA-Z0-9\-]|';    $palavranova = preg_replace($pattern, ' ', $palavranova);
        $string = str_replace(' ', '-', $palavranova);
        $string = str_replace('---', '-', $string);
        $string = str_replace('--', '-', $string);
        return strtolower($string);
    }

For example

César Sousa

You should convert to cesar-Sousa But it’s converting like this c-Sar-Sousa

2 answers

4


To clear anything you want to put in a URL you can use the following function (which uses iconv):

function sanitize_title($title) {
    // substitui espaços por "-"
    $title = preg_replace('#\s+#', '-', $title);

    // faz a transliteração pra ASCII
    $title = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $title);

    // remove qualquer outra coisa inválida da url
    $title = preg_replace('#[^a-zA-Z0-9_-]+#', '', $title);

    return $title;
}

Before calling this function it is good that you use setlocale for transliteration to work correctly:

setlocale(LC_ALL, 'pt_BR.UTF-8');

Example: echo sanitize_title('César Sousa'); echo sanitize_title('áéíóú@#_888999-test other words');

Gera:

cesar-sousa
aeiou_888999-teste-outras-palavras

See a test on ideone

  • Really with the function that @P. Santos put worked but in some I noticed that it was not working well but with this one it worked right thanks to the two for the help

0

A simple function that works with me:

function url_amigavel($string){
    return preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities($string));
}

Something more complete would be:

function url_amigavel($string){
    $url = str_replace(' ', '_', $string);
    $url = preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities($url));

    return strtolower($url);
}

See here the test:

inserir a descrição da imagem aqui

I hope it works

Abs

  • I tested it here but it didn’t work it doesn’t convert any character

  • Hello @Césarsousa, I added an image with the test done with success. Take a look.

  • I tested with this new more complete and worked well. thanks had already tested many and no solved

  • Depending on what happens, htmlentities will generate special characters and the URL will be broken.

  • @Andréribeiro could please cite an example where this could happen so I can test here?

  • 1

    @P.Santos Take a look in this test on ideone

Show 1 more comment

Browser other questions tagged

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