使用短划线显示URL slug时出现问题

Mac*_*lor 4 php url slug

我用破折号为我的故事网址制作了一个slug,例如:

使用slug而不是ID获取记录

这是我创建slug的代码:

function Slugit($title) {
    $title = strip_tags($title);
    // Preserve escaped octets.
    $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
    // Remove percent signs that are not part of an octet.
    $title = str_replace('%', '', $title);
    // Restore octets.
    $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);


    $title = remove_accents($title);
    if (seems_utf8($title)) {
        if (function_exists('mb_strtolower')) {
            $title = mb_strtolower($title, 'UTF-8');
        }
        $title = utf8_uri_encode($title, 500);
    }

    $title = strtolower($title);
    $title = preg_replace('/&.+?;/', '', $title); // kill entities
    $title = str_replace('.', '-', $title);
    $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
    $title = preg_replace('/\s+/', '-', $title);
    $title = preg_replace('|-+|', '-', $title);
    $title = trim($title, '-');


    return $title;
}
Run Code Online (Sandbox Code Playgroud)

你可以看到破折号,到这里,一切都很好.但是当我点击链接时,它无法打开并找到它我的数据库,因为它保存在正常情况下,没有破折号.

所以我写了一些东西来删除破折号:

$string = str_replace('-', ' ', $string);
Run Code Online (Sandbox Code Playgroud)

但是当有?或者.在URL中时,它就无法显示!

有什么帮助来找回原始网址?!

Pas*_*TIN 8

如果您有这样的URL:

https://stackoverflow.com/questions/2647789/problem-in-displaying-a-slug-with-dash
Run Code Online (Sandbox Code Playgroud)

problem-in-displaying-a-slug-with-dash部分不是很重要的:它只是不错:

  • 显示
  • 供用户阅读,看看有什么问题
  • 对于搜索引擎,作为SEO机制.


在该URL中,真正重要的2647789部分:它是数据库中问题标识符 - 即它是用于加载问题的URL的一部分.

这意味着无需将slug转换为用户首次输入的内容:唯一重要的是您在每个URL中传递数据的标识符,并使用它来找回您的数据.


并且,如果你想要某种证据:尝试没有slug的情况下显示带有破折号的URL slu的问题:你会看到你的问题加载得很好;-)

  • 它不仅不重要,而且被忽略了!您可以查看[http://stackoverflow.com/questions/2647789/this-is-NOT-the-title](http://stackoverflow.com/questions/2647789/this-is-NOT-the-title)精细!:) (4认同)