如何“解除”标题

发布于 2024-09-18 07:14:42 字数 565 浏览 8 评论 0原文

我正在创建一个需要 slug 系统的系统。例如,标题为“博客标题:这里”的博客文章的 URL 将为:

YYYY/MM/DD/the-blog-title-here

我已经将系统设置得很好,如果您的 URL 匹配多个如果它只匹配一个条目,它会显示所有匹配条目的摘要,如果它只匹配一个条目,则会显示完整条目,依此类推。我遇到的唯一麻烦是标题的“不打架”。目前,我只考虑第一个单词:我的 SQL 查询结束如下:

'AND subject LIKE '.$this->db->escape(substr($title,0,strpos($title,'-')).'%')

问题是,如果同一天发布的两个帖子以相同的单词开头,则单个条目永远无法匹配。此外,如果一个帖子以单词“A”开头,而同一天发布的另一个帖子以字母 A 开头,则前一个帖子永远无法单独匹配。

我最初的想法是尝试将 slug 中的每个单词与 LIKE %word% 相匹配,虽然这样更好,但看起来很黑客而且仍然不完美(“蛞蝓:麻烦!”和“麻烦的蛞蝓”会发生冲突)。这个问题有优雅的解决方案吗?

I'm creating a system which requires a slug system. For example, the URL for a blog post entitled "The Blog Title: Here" would be:

YYYY/MM/DD/the-blog-title-here

I have the system pretty well set up, where if your URL matches more than one entry it displays summaries of all matched entries, and if it only matches one entry it displays the full entry and so on. The only trouble I'm running into is with the "unslugging" of the title. Currently, I only take the first word into account: my SQL query ends like this:

'AND subject LIKE '.$this->db->escape(substr($title,0,strpos($title,'-')).'%')

The trouble is, if two posts posted on the same day begin with the same word, the single entry can never be matched. Furthermore, if one post begins with the word 'A' and another post posted on the same day begins with the letter A, the former post can never be matched alone.

My initial thought was to try to match every word in the slug with a LIKE %word%, and while this is better it seems hackish and still isn't perfect ("A slug: troublesome!" and "A troublesome slug" would conflict). Is there an elegant solution to this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-09-25 07:14:42

为什么不在表中创建一个“slug”字段并对其进行匹配呢?将 slug 完全按照 url 中显示的方式存储在那里,并进行完全匹配。应该可以解决你的问题。另外,在数据库中输入slug时,检查是否存在类似的slug,如果存在,则在末尾添加一个数字。例如“我的帖子”、“我的帖子1”。

因此,这会在插入时涉及一些计算,但应该可以解决您的精确匹配问题。

Why not create a "slug" field in the table and just match on that? Store the slug in there exactly as it would be displayed in the url and do an exact match. Should solve your problem. Also when entering the slug in the database, check if a similar slug exists, if it does, add a number to the end. for instance 'my-post', 'my-post-1'.

So this would involve a little calculation at the insertion time but should solve your exact match issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文