Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building SEO-friendly URLs for accented characters

We are making our site an SEO-friendly site by following the pattern below:

http://OurWebsite.com/MyArticle/Math/Spain/Glaño

As you see, Glaño has a spelling character that search engines may not like it. On the other hand we cannot build up the last URL!

Any suggestions to maintain our current URL generation code to handle Spanish or French entries or we need to change our approach?

like image 802
Arash Avatar asked Dec 28 '22 13:12

Arash


1 Answers

Try these functions:

function Slug($string, $slug = '-', $extra = null)
{
    return strtolower(trim(preg_replace('~[^0-9a-z' . preg_quote($extra, '~') . ']+~i', $slug, Unaccent($string)), $slug));
}

function Unaccent($string)
{
    return html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8');
}

And use it like this:

echo Slug('Iñtërnâtiônàlizætiøn of Glaño'); // internationalizaetion-of-glano

You can embed the Unaccent() code into the Slug() function if you wish to have only one function.

like image 129
Alix Axel Avatar answered Jan 05 '23 00:01

Alix Axel