php - Make URLs prettier?
迷茫
迷茫 2017-06-12 09:21:22
0
1
774

I want to render the Chinese URL
and then convert some special characters into "-"

$patten = array(
        '!',
        '!',
        '?',
        '?',
        '~',
        '~',
        '「',
        '」',
        ',',
        ',',
        '.',
        '。'
    );
$zh_url = str_replace(' ','',str_replace($patten,'-',strtolower($title)));

Like this
Only two "--" sometimes appear
How to make the string automatically become "one" when there are two or more (inclusive) "--"
xx -x--xxxx--xxx
becomes
xx-x-xxxx-xxx

Then if there is more than one "-" at the end of the string, remove it?
For example, xxxxxxx-xxx-- becomes xxxxxxx-xxx

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
Peter_Zhu
$zh_url = preg_replace('#-{2,}#', '-', trim(str_replace(' ', '', str_replace($patten, '-', strtolower($title))), '-'));

update:

$zh_url = preg_replace('#-{2,}#', '-', preg_replace('#^-{2,}|-{2,}$#', '', str_replace(' ', '', str_replace($patten, '-', strtolower($title)))));
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!