去除php变量,用破折号替换空格
2022-08-30 08:29:15
如何将 PHP 变量从“我的公司和我的名字”转换为“我的公司我的名字”?
我需要将其全部小写,删除所有特殊字符并用破折号替换空格。
如何将 PHP 变量从“我的公司和我的名字”转换为“我的公司我的名字”?
我需要将其全部小写,删除所有特殊字符并用破折号替换空格。
此函数将创建一个 SEO 友好字符串
function seoUrl($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
应该没:)
是的,如果你想处理任何特殊字符,你需要在模式中声明它们,否则它们可能会被冲走。你可以这样做:
strtolower(preg_replace('/-+/', '-', preg_replace('/[^\wáéíóú]/', '-', $string)));