【问题标题】:How to handle a dash in your url when replacing spaces with dashes用破折号替换空格时如何处理网址中的破折号
【发布时间】: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


【解决方案1】:

你可以像这样替换多个分隔符:

$string = preg_replace("/-+/", "", $string);

在你的函数上下文中:

<?php

echo slug("Foo - Bar"); // foo-bar
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);
    //delete multiple separator
    $string = preg_replace("/".$spaceRepl."+/", "", $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;
}

编辑:

或者你可以像这样改变你的 str_replace

<?php

echo slug("Foo -    Bar"); // foo-bar
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 = preg_replace("/\s+/", "-", $string); // new way
    //$string = str_replace(" ", $spaceRepl, $string); // old way
    return $string;
}

【讨论】:

  • 第一个选项完美!第二个没有,但没关系。非常感谢@Samy
【解决方案2】:

我为干净的蛞蝓制作了这个功能。它将所有多个破折号替换为单个破折号,删除所有特殊字符。也许对你有帮助。

function clean($string) {
    $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
    $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

    return strtolower(preg_replace('/-+/', '-', $string)); // Replaces multiple hyphens with single one.
}

echo clean("Title - Of The Page");

Demo

注意:也许它不是那么理想,所以这个答案是开放的建议

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多