我用破折号为我的故事网址制作了一个slug,例如:
这是我创建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中时,它就无法显示!
有什么帮助来找回原始网址?!
如果您有这样的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
部分不是很重要的:它只是不错:
在该URL中,真正重要的是2647789
部分:它是数据库中问题的标识符 - 即它是用于加载问题的URL的一部分.
这意味着无需将slug转换为用户首次输入的内容:唯一重要的是您在每个URL中传递数据的标识符,并使用它来找回您的数据.
并且,如果你想要某种证据:尝试在没有slug的情况下显示带有破折号的URL slu的问题:你会看到你的问题加载得很好;-)