problem in displaying a slug with dash

Posted by Mac Taylor on Stack Overflow See other posts from Stack Overflow or by Mac Taylor
Published on 2010-04-15T18:00:26Z Indexed on 2010/04/15 18:03 UTC
Read the original article Hit count: 525

Filed under:
|

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 dispaly !

any help to retrieve back the original url ?!

© Stack Overflow or respective owner

Related posts about php

Related posts about slug