How to handle diacritics (accents) when rewriting 'pretty URLs'

前端 未结 6 802
长情又很酷
长情又很酷 2020-11-30 09:48

I rewrite URLs to include the title of user generated travelblogs.

I do this for both readability of URLs and SEO purposes.

 http://www.example.com/gall         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 10:36

    This is a good function:

    function friendlyURL($string) {
        setlocale(LC_CTYPE, 'en_US.UTF8');
        $string = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $string);
        $string = str_replace(' ', '-', $string);
        $string = preg_replace('/\\s+/', '-', $string);
        $string = strtolower($string);
        return $string;
    }
    

提交回复
热议问题