Elegant solution
With preg_replace you can get this with a single line of code:
preg_replace('#/+#','/',$str);
The /+ pattern will match the forwardlash / character one or more times and replace it with one / .
Not very elegant solution
Of course, there are other ways to achieve this, for example, using a while .
while( strpos($path, '//') !== false ) { $path = str_replace('//','/',$path); }
This will call str_replace until all // entries are replaced. You can also write this loop in one line of code if you want to sacrifice readability (not recommended).
while( strpos( ($path=str_replace('//','/',$path)), '//' ) !== false );
source share