且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为重音字符构建SEO友好的URL

更新时间:2023-11-07 23:03:34

尝试以下功能:

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');
}

并像这样使用它:

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

如果只希望有一个功能,可以将Unaccent()代码嵌入到Slug()函数中.

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