为重音字符构建SEO友好的URL [英] Building SEO-friendly URLs for accented characters

查看:102
本文介绍了为重音字符构建SEO友好的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们通过遵循以下模式将我们的网站打造为SEO友好型网站:

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

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

如您所见,Glaño具有一个拼写字符,搜索引擎可能不喜欢它.另一方面,我们无法建立最后一个URL!

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!

是否有任何建议保持我们当前的URL生成代码以处理西班牙文或法文条目,或者我们需要更改方法?

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

推荐答案

尝试以下功能:

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.

这篇关于为重音字符构建SEO友好的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆