Remove unnecessary slashes from the path

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

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

I am trying to get these results

 /home/to/my/site 

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

+4
source share
6 answers

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 ); 
+24
source

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

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

(thanks to ツ Liverbool how to remove multiple slashes in a URI using "PREG" or "HTACCESS" )

+5
source

Hi, maybe it will be useful

Write this code in the .Htaccess file and verify it.

 # Prevent double slashes in URLs, eg //Blog and /Home//About RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ RewriteRule . %1/%2 [R=301,L] 

Hope this helps you!

+2
source

You can use the built-in realpath () function to remove slashes of existing files. But you will always have a canonized absolute path.

 <?php // 2 slashes echo realpath('/etc//passwd') . PHP_EOL; // prints /etc/password // 3 slashes echo realpath('/etc///passwd') . PHP_EOL; // prints /etc/password // 2 .. echo realpath('/etc/../etc/passwd') . PHP_EOL; // prints /etc/password ?> 

Note that this function returns an error if the file does not exist .

Some important notes from docs :

realpath () expands all symbolic links and resolves references to '/./', '/../' and additional characters '/' in the input path and returns the canonized absolute path.

and

In windows, realpath () changes the unix style paths to the Windows style.

+1
source

while(strlen($path) != (strlen($path = str_replace('//','/', $path))));

This code replaces double slashes with a single slash if it changes length;

0
source

It replaces the (sequential) occurrences of / and \ c with everything that is in DIRECTORY_SEPARATOR, and / processes. and / .. shallow. The paths returned by get_absolute_path () do not contain (backward) at position 0 (start of line) or position -1 (end)

 function get_absolute_path($path) { $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutes = array(); foreach ($parts as $part) { if ('.' == $part) continue; if ('..' == $part) { array_pop($absolutes); } else { $absolutes[] = $part; } } return implode(DIRECTORY_SEPARATOR, $absolutes); } 

Test:

 var_dump(get_absolute_path('this/is/../a/./test/.///is')); 

Returns: string (14) "this/a/test/is"

0
source

Source: https://habr.com/ru/post/1435055/


All Articles