Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unnecessary slashes from path

Tags:

php

$path = '/home/to//my///site';

I am trying to remove unnecessary forward slashes / from the path above

I am trying to get this results

/home/to/my/site

I've failed with str_replace, since i don't know the number of slashes.

like image 325
Alaa Gamal Avatar asked Sep 19 '12 12:09

Alaa Gamal


1 Answers

if someone wants to remove extra slashes from URL without removing first two slashes after http/https:

$url = preg_replace('/([^:])(\/{2,})/', '$1/', $url); 

(thanks to ツ Liverbool how to remove multiple slashes in URI with 'PREG' or 'HTACCESS')

like image 137
Alexandr Suleymanov Avatar answered Sep 21 '22 17:09

Alexandr Suleymanov