Jas*_*vis 23 php string friendly-url slug
在我的PHP站点上,当前用户使用电子邮件地址和密码登录.我想添加一个用户名,他们设置的用户名将是唯一的,他们无法更改.我想知道如何使这个名称没有空格并在URL中工作,所以我可以使用用户名链接到配置文件和其他东西.如果用户名中有空格,则应添加下划线jason_davis.我不确定最好的方法吗?
Ali*_*xel 93
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')), '-'));
}
$user = 'Alix Axel';
echo Slug($user); // alix-axel
$user = 'Álix Ãxel';
echo Slug($user); // alix-axel
$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix-axel
Run Code Online (Sandbox Code Playgroud)