0

I need code to remove the trailing slash when a user enters their link. For example I need them to put their url to grab their avatar, but in some cases they put their url ending with a slash (.com/). How can I remove that slash automatically? Because when they put their url like that, the avatar doesn't show.

I was looking to remove a trailing slash with php code, but any solution will be appreciated.

I tried to use this code but didn't work:

$string = rtrim($string, '/');
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
Gio luna
  • 21
  • 6

2 Answers2

1

Use substring(-1). If it equals "/", then remove it

if (substr($urlString, -1) == "/") $urlString = substr($urlString, 0, -1);

RandomDuck.NET
  • 490
  • 5
  • 17
0

The function rtrim just removes whitespace form the end of the string. You should check if the last character in the string is a "/" and then remove it if it is.

One way to do this would be:

if (substr_compare($string, "/", -1) == 0) { 
    $string = substr($string, 0, -1);
}
Giles
  • 362
  • 2
  • 9
  • i'm having the same issue, should i put it in the plug in? or in the text box? this is the plug in.. static function getProfileImage($screenname, $size = 128) { $url = self::API_URL . $screenname . '/avatar/' . $size; – Gio luna Jun 21 '12 at 03:23
  • and the text box is another page – Gio luna Jun 21 '12 at 03:25
  • Incorrect. `rtrim()` accepts a second option argument `$charList` that takes a string of chars that it should trim from the right of the string along with whitespace. – Bailey Parker Jun 21 '12 at 03:32