我正在尝试编写一个简单的 PHP 函数,它可以接受像这样的字符串
Topic: Some stuff, Maybe some more, it's my stuff?
并返回
topic-some-stuff-maybe-some-more-its-my-stuff
As such:
- 小写
- 删除所有非字母数字非空格字符
- 用连字符替换所有空格(或空格组)
我可以用一个正则表达式来做到这一点吗?
function Slug($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', 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')), '-'));
}
$topic = 'Iñtërnâtiônàlizætiøn';
echo Slug($topic); // internationalizaetion
$topic = 'Topic: Some stuff, Maybe some more, it\'s my stuff?';
echo Slug($topic); // topic-some-stuff-maybe-some-more-it-s-my-stuff
$topic = 'here عربي Arabi';
echo Slug($topic); // here-arabi
$topic = 'here 日本語 Japanese';
echo Slug($topic); // here-japanese
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)