how to remove multiple slashes in URI with 'PREG' or 'HTACCESS'

后端 未结 5 2091
滥情空心
滥情空心 2021-01-04 21:51

how to remove multiple slashes in URI with \'PREG\' or \'HTACCESS\'

site.com/edition/new/// -> site.com/edition/new/


site.com/edition///new/ -> site.co

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 22:28

    using the plus symbol + in regex means the occurrence of one or more of the previous character. So we can add it in a preg_replace to replace the occurrence of one or more / by just one of them

       $url =  "site.com/edition/new///";
    
    $newUrl = preg_replace('/(\/+)/','/',$url);
    
    // now it should be replace with the correct single forward slash
    echo $newUrl
    

提交回复
热议问题