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...
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.
apply rtrim
like so
$string = rtrim($string, '/');
$string = rtrim($string, '/');
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)
There are places where /
can be duplicated, for example, you can access your question through all these links:
- Remove duplicate trailing slashes
- Remove duplicate trailing slashes
- https://stackoverflow.com///questions///13990256///remove-duplicate-trailing-slashes////
- https://stackoverflow.com/questions//13990256/remove-duplicate-trailing-slashes//
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);