用破折号显示 URL slug 的问题 [英] Problem in displaying a URL slug with dash
问题描述
我为我的故事 URL 制作了一个带破折号的 slug,例如:
I made a slug with dash for my stories URLs such as:
这是我创建 slug 的代码:
This is my code to create 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;
}
正如你所看到的破折号,到这里,一切都很好.但是当我点击链接时,它无法打开并找到我的数据库,因为它以正常方式保存并且没有破折号.
As you can see dashes, up to here, everything is fine. But when I click on the link, it can not open and find it my database as it's saved in normal and with no dashes.
所以我写了一些东西来删除破折号:
So I wrote something to remove dashes:
$string = str_replace('-', ' ', $string);
但是当URL中有?
或.
时,就不能显示了!
But when there is ?
or .
in URL, then it can not display!
找回原始网址有什么帮助吗?!
Any help to retrieve back the original URL?!
推荐答案
当你有这样一个 URL 时:
When you have an URL such as this one :
https://stackoverflow.com/questions/2647789/problem-in-displaying-a-slug-with-dash
problem-in-displaying-a-slug-with-dash
部分不是很重要:它只是很好:
The problem-in-displaying-a-slug-with-dash
part is not quite important : it's just nice :
- 显示
- 供用户阅读,并了解问题的内容
- 对于搜索引擎,作为 SEO 机制.
真正重要的是,在那个 URL 中,是 2647789
部分:它是数据库中问题的标识符-- 即用于加载问题的 URL 部分.
What's really important, in that URL, is the 2647789
part : it's the identifier of the question in the database -- i.e. it's that part of the URL that's used to load the question.
这意味着无需将 slug 转换为用户最初输入的内容:唯一重要的是您在每个 URL 中传递数据的标识符,并使用它来查找数据.
This means there is no need to convert the slug to what was first typed by the user : only thing that matters is that you pass the identifier of your data in each URL, and use it to find your data back.
而且,如果您想要某种证据:尝试去显示带有破折号的 URL slu 的问题 没有 slug :你会看到你的问题加载得很好;-)
And, if you want some kind of proof : try going to Problem in displaying a URL slug with dash without the slug : you'll see that your question loads just fine ;-)
这篇关于用破折号显示 URL slug 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!