Php 用破折号显示URL段塞时出现问题
我用破折号写了一篇故事,比如: 这是我创建slug的代码:Php 用破折号显示URL段塞时出现问题,php,url,slug,Php,Url,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 a
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
用破折号显示弹头时出现的问题不太重要:它很好:
- 显示
- 供用户阅读,并了解问题的内容
- 对于搜索引擎来说,这是一个SEO迷
真正重要的,在该URL中,是2647789
部分:它是数据库中问题的标识符——即用于加载问题的URL部分
这意味着不需要将slug转换为用户首先键入的内容:唯一重要的是在每个URL中传递数据的标识符,并使用它来查找数据
而且,如果你想得到一些证据:试着不带鼻涕虫去:你会发现你的问题负载很好;-) 如果没有数字ID,只使用slug来标识数据库中的记录。然后,您也可以简单地将slug存储在数据库中,并使用该slug查询它。它不仅不重要,而且被忽略!你可以看得很好!:)问题是我并没有使用标识符:所以你们说并没有办法找到原来的url了?因为wordpress正在这么做,但我不知道怎么做@Mac:如果你真的不想存储标识符,你可以使用“干净”URL作为标识符:将“干净”URL作为附加字段存储在数据库中,然后使用该字段取回数据。谢谢,还有一个问题,为什么这个网站不在URL末尾使用斜杠,但wordpress会这样做,这很重要吗?欢迎:-);;;不,我认为最后的/
一点都不重要——好吧,也许wordpress期待它,但是,在这种情况下,它是wordpress团队做出的选择,而不是某种标准。在这里查看我的答案