从文章标题创建 URL Slug

Avatar of Chris Coyier
Chris Coyier

替换单词之间空格为连字符的正则表达式函数。

<?php
function create_slug($string){
   $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>