Remove Duplicate Slashes

I would like to detect with php if a string like $string includes duplicates of trailing slashes.

For instance:

 $string = "http://somepage.com/something/some.html/////"; 

to

 $string = "http://somepage.com/something/some.html"; 

And I want to do if if it has a duplicate, something like:

 If ($string = "http://somepage.com/something/some.html/////";) { remove extra trailing slashes } //else do nothing... 
+3
source share
5 answers

You can simply use rtrim() :

 $string = rtrim($string, '/'); 

If for some reason you want to check first if it has a trailing slash, then you can check the last character, for example:

 if ($string[ strlen($string)-1 ] === '/') { $string = rtrim($string, '/'); } 

Throwing a line through rtrim() not expensive, so you do not need to check for slashes first.

Using regular expressions to trim trailing slashes is a bit overpriced.

+4
source

apply rtrim so

 $string = rtrim($string, '/'); 
+9
source
 $string = rtrim($string, '/'); 
+3
source

rtrim is the best solution, but since you're completeness of the regex tag:

 $string = "http://somepage.com/something/some.html/////"; echo preg_replace('#/+$#','',$string); >>> http://somepage.com/something/some.html 

 # - Is the delimiter character /+ - Matches one or more forward slash $ - Matches the end of the string # - Delimiter Replace with '' - Nothing (empty string) 
+3
source

There are places where / can be duplicated, for example, you can access your question through all these links:

The only double / that matters here is http:// , so consider it. rtrim will not work on rtrim own in most cases that I have provided, so release regular expressions.

Decision

 $parts = explode('//', $full_url, 2); $parts[1] = rtrim(preg_replace('@/ +@ ', '/', $parts[1]), '/'); $full_url = implode('//', $parts); unset($parts); 

Live test: http://ideone.com/1qHR9o

 Before: /questions/1460693/remove-duplicate-trailing-slashes After: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes --------------------- Before: /questions/1460693/remove-duplicate-trailing-slashes/// After: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes --------------------- Before: https://stackoverflow.com///questions///13990256///remove-duplicate-trailing-slashes//// After: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes --------------------- Before: https://stackoverflow.com/questions//13990256/remove-duplicate-trailing-slashes// After: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes --------------------- 

Description

From your question, I understand that you always get the full URL, so we can split it into two parts:

 $parts = explode('//', $full_url, 2); 

Now we will remove the duplicated / using:

 preg_replace('@/ +@ ', '/', $parts[1]) 

Then we remove the extra / from the end of the line:

 $parts[1] = rtrim( /*previous line*/ , '/'); 

And bring it back:

 $full_url = implode('//', $parts); unset($parts); 
+3
source

All Articles