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

前端 未结 3 1690
南旧
南旧 2021-02-14 21:43

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.

3条回答
  •  不知归路
    2021-02-14 22:17

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

提交回复
热议问题