how to "un-slug" a title slug
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 see开发者_运维问答ms hackish and still isn't perfect ("A slug: troublesome!" and "A troublesome slug" would conflict). Is there an elegant solution to this problem?
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.
精彩评论