Convert any title to url slug and back from url slug to title

Upvote picture Upvote · Dec 24, 2010 · Viewed 13k times · Source

I want to convert any title e.g. of a blog entry to a user friendly url. I used rawurlencode() to do that but it gives me a lot of strange strings like %s.

The algorithm should consider german chars like Ö, Ä, etc. I want to make a url from title and be able to get the title by decoding the url.

I tried some of this code: http://pastebin.com/L1SwESBn that is provided in some other questions but it seems to be one way.

E.g. HÖRZU.de -> hoerzu-de -> HÖRZU.de

Any ideas?

Answer

Hannes picture Hannes · Dec 24, 2010

You want to create slugs, but from experience i can tell you the decode possibilities are limited. For example "Foo - Bar" will become "foo-bar" so how do you then can possibly know that it wasn't "foo bar" or "foo-bar" all along?

Or how about chars that you don't want in your slug and also have no representation for like " ` "? So you can ether use a 1 to 1 converstion like rawurlencode() or you can create a Slug, here is an example for a function - but as i said, no reliable decoding possible - its just in its nature since you have to throw away Information.

function sanitizeStringForUrl($string){
    $string = strtolower($string);
    $string = html_entity_decode($string);
    $string = str_replace(array('ä','ü','ö','ß'),array('ae','ue','oe','ss'),$string);
    $string = preg_replace('#[^\w\säüöß]#',null,$string);
    $string = preg_replace('#[\s]{2,}#',' ',$string);
    $string = str_replace(array(' '),array('-'),$string);
    return $string;
}