正则表达式函数,用连字符替换单词之间的空格。
<?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'
?>
翻译自: https://css-tricks.com/snippets/php/create-url-slug-from-post-title/