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

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


$url = 'http://www.abc.com/def/git//ss';$url = preg_replace('/([^:])(\/{2,})/', '$1/', $url);// output http://www.abc.com/def/git/ss$url = 'https://www.abc.com/def/git//ss';$url = preg_replace('/([^:])(\/{2,})/', '$1/', $url);// output https://www.abc.com/def/git/ss


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 slashecho $newUrl


Simple, check this example :

$url ="http://portal.lojav1.local//Settings////messages";echo str_replace(':/','://', trim(preg_replace('/\/+/', '/', $url), '/'));

output :

http://portal.lojav1.local/Settings/messages