search

Home  >  Q&A  >  body text

php - Make URLs prettier?

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

迷茫迷茫2731 days ago806

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-06-12 09:23:22

    $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)))));

    reply
    0
  • Cancelreply