Lelis Lelis - 4 months ago 22
PHP Question

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

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

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




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

thanks

Ibu Ibu
Answer

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