【发布时间】:2017-10-17 07:33:38
【问题描述】:
我目前将我的 htaccess 设置为:
RewriteRule ^post/([0-9]+)/([a-zA-Z0-9_-]+) post.php?id=$1
这会将我的链接显示为
post/476/title-of-the-page
但是,如果我的标题中有 -,它会显示三个
Title - Of The Page
变成
post/476/title---of-the-page
这是我目前处理链接的功能,但我不确定如何正确处理
function slug($string, $spaceRepl = "-") {
// Replace "&" char with "and"
$string = str_replace("&", "and", $string);
// Delete any chars but letters, numbers, spaces and _, -
$string = preg_replace("/[^a-zA-Z0-9 _-]/", "", $string);
// Optional: Make the string lowercase
$string = strtolower($string);
// Optional: Delete double spaces
$string = preg_replace("/[ ]+/", " ", $string);
// Replace spaces with replacement
$string = str_replace(" ", $spaceRepl, $string);
return $string;
}
我可以更改我的preg_replace 以删除-s,但有些帖子将它们用于不同的目的。
【问题讨论】:
-
1.删除任何破折号。 2. 用一个空格替换任何连续的空格。 3. 用破折号代替空格……
标签: php .htaccess url mod-rewrite