编程语言
首页 > 编程语言> > php – 显示带有破折号的URL slug时出现问题

php – 显示带有破折号的URL slug时出现问题

作者:互联网

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

Fetching records with slug instead of 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;
}

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

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

$string = str_replace('-', ' ', $string);

但是什么时候有?要么 .在URL中,则无法显示!

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

解决方法:

如果您有这样的URL:

https://stackoverflow.com/questions/2647789/problem-in-displaying-a-slug-with-dash

显示问题 – 一个slu-with-dash部分并不是很重要:它很不错:

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

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

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

并且,如果你想要某种证据:试着去Problem in displaying a URL slug with dash没有slu ::你会看到你的问题加载得很好;-)

标签:php,url,slug
来源: https://codeday.me/bug/20190610/1213502.html