Problem displaying URL pool with dash

I made a slug with a dash for my URL stories, such as:

Retrieving records using slug instead of ID

This is my code for creating 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 the dash, everything is still fine. But when I click on the link, it cannot open and find it in my database, because it is saved in normal mode and without a dash.

So, I wrote something to remove the dash:

 $string = str_replace('-', ' ', $string); 

But when is the URL ? or . , it cannot be displayed!

Any help for returning the original URL ?!

+4
source share
2 answers

If you have a URL like this:

 /questions/1307066/problem-in-displaying-a-url-slug-with-dashproblem-in-displaying-a-slug-with-dash 

The problem-in-displaying-a-slug-with-dash not very important: it's just nice:

  • to display
  • so that users can read and see what the question is about
  • for a search engine like SEO mech.


What is really important , in this URL is the 2647789 part : this is the identifier of the question in the database - that is, the part of the URL that was used to load the question.

This means that there is no need to convert the pool to what was first entered by the user: it is only important that you pass the identifier of your data in each URL and use it to search for your data.


And if you need some confirmation: try The problem of displaying a URL pool with a dash without a bullet: you will see that your question loads just fine; -)

+8
source

If you do not have a numeric identifier and use only the pool to identify the record in your database. Then you can simply save the pool to the database and query it using that pool.

0
source

Source: https://habr.com/ru/post/1307066/