Remove duplicate trailing slashes

2019-07-03 22:39发布

I would like to detect with php if a string like $string include duplicate trailing slashes.

For example:

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

to

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

And I wanna do an if, if it has duplicate, something like:

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

5条回答
对你真心纯属浪费
2楼-- · 2019-07-03 22:57

apply rtrim like so

$string = rtrim($string, '/');
查看更多
Bombasti
3楼-- · 2019-07-03 23:04

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

The only double / that makes difference here is the http://, so let's consider it. rtrim alone will not work in most of the cases I provided, so let's go with regular expressions.

Solution

$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: 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
---------------------
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
---------------------

Explanation

From your question I understand that you always get a complete URL, so, we can split it in two parts:

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

Now we remove the duplicated / with:

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

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

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

And implode it back:

$full_url = implode('//', $parts);
unset($parts);
查看更多
Explosion°爆炸
4楼-- · 2019-07-03 23:06
$string = rtrim($string, '/');
查看更多
放荡不羁爱自由
5楼-- · 2019-07-03 23:10

rtrim is the best solution but since you tagged regex for completeness:

$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)
查看更多
聊天终结者
6楼-- · 2019-07-03 23:23

You can just use rtrim():

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

If you for some reason want to first check if it has trailing slashes then you can check the last character, like so:

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

Throwing the string through rtrim() is not expensive so you do not really have to check for trailing slashes first.

Using regular expressions to trim trailing slashes is a little over-kill.

查看更多
登录 后发表回答