tags:

views:

173

answers:

2

Hey guys

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

http://stackoverflow.com/questions/482636/fetching-records-with-slug-instead-of-id

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);

But when there is ? or . in URL, then it can not display!

Any help to retrieve back the original URL?!

+5  A: 

When you have an URL such as this one :

http://stackoverflow.com/questions/2647789/problem-in-displaying-a-slug-with-dash

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

  • to display
  • for users to read, and see what the question's about
  • for search engine, as a SEO mecanism.


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.

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.


And, if you want some kind of proof : try going to http://stackoverflow.com/questions/2647789/ without the slug : you'll see that your question loads just fine ;-)

Pascal MARTIN
Not only is it not important, it's ignored! You can view [http://stackoverflow.com/questions/2647789/this-is-NOT-the-title](http://stackoverflow.com/questions/2647789/this-is-NOT-the-title) just fine! :)
Joshua
problem is here i used no identifier :so u r saying there is no way to find original url back ?because wordpress is doing so , but i cant find out how !?
Mac Taylor
@Mac : if you really don't want to store an identifier, you could use the "clean" URL as an identifier : store the "clean" URL as an additionnal field in your DB, and use that field to get your data back.
Pascal MARTIN
thanks , one more question , why this site doesnt use slash at the end of urls but wordpress does so , is it important ?
Mac Taylor
You're welcome :-) ;;; no, I don't think the final `/` is important at all -- well, maybe wordpress expects it, but, in this case, it's a choice made by wordpress' team, and not fixed by some kind of standard.
Pascal MARTIN
but i still , didn't find out , how wordpress can understand what is this url refer to :www.mysite.com/about-me/they are using no identifier if they using slug function so how they can retain story information
Mac Taylor
A: 

If you don’t have an numeric ID and use just the slug to identify the record in your database. Then you could simply store the slug in the database too and query it with that slug.

Gumbo