3

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... 
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
János Tigyi
  • 397
  • 1
  • 9
  • 20

5 Answers5

9

apply rtrim like so

$string = rtrim($string, '/');
Crisp
  • 11,417
  • 3
  • 38
  • 41
6

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.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • if ($string[ strlen($string)-1 ] === '/') { $string = rtrim($string, '/'); } .. What does $string[ strlen($string)-1 ] means... Why is $string an array? ... I don't understood, but it looks like the best solution for me. I have two types of URL... for example: http://mikrobusz-berles.com and http://mikrobusz-berles.com/flotta.html ... and I just wanna to remove ebery extra slashes... at .html it is easy, but what's about when i write: http://mikrobusz-berles.com/////////// ? – János Tigyi Dec 21 '12 at 14:51
  • 1
    You can access characters inside strings using `[]`. `rtrim($string, '/')` removes all forward slashes on the right. See the [documentation](http://php.net/string#language.types.string.substr) before asking more questions. – Sverri M. Olsen Dec 21 '12 at 14:59
3
$string = rtrim($string, '/');
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
3

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)
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
3

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);
Community
  • 1
  • 1
Polyana Fontes
  • 3,156
  • 1
  • 27
  • 41
  • I have no idea, I voted it up for you to compensate :) The explode part is bit tricky though. I would prefer an exclude regex, which simply excludes the `https?://` part. Because, what if there is no protocol defined in the url? Then it will fail. – Jelmer Dec 21 '12 at 13:06
  • @Jelmer actually, if there's no protocol defined, then you don't have an URL, you have an [URI](http://stackoverflow.com/questions/176264/whats-the-difference-between-a-uri-and-a-url) and the data would be invalid, but it can be easily tested by checking if the size of the array is 2, although I test if the parameters are valid when I receive then. – Polyana Fontes Dec 21 '12 at 13:15
  • All the OP wants is to remove **trailing** forward slashes in a string which `rtrim` is the perfect solution for, the question you have answered is validating forward slashes in a `URL`. – Chris Seymour Dec 21 '12 at 13:16
  • @sudo_O ok, I didn't know trailing means "at right", I always understood it as multiple or in sequence. That was a language barrier. – Polyana Fontes Dec 21 '12 at 13:22
  • @JoséRobertoAraújoJúnior Ahh okay yes `trailing` means `found at the end` – Chris Seymour Dec 21 '12 at 13:24
  • Ah! Explode/implode. Thank you :) I found out my `sanitizePath()` function *accidentaly* removed double-slashes in `http://`, too, which was an unwanted behaviour. I was looking for a simple and short solution and found this. Excellent :) – Smuuf Oct 07 '13 at 01:55