Removing a forward-slash from the tail-end of an URL

You can pass a string of characters that you want trimmed off of a string to the trim family of functions. Also, you could use rtrim to trim just the end of the string:

$site = rtrim($site, "/");

$site = preg_replace('{/$}', '', $site);

This uses a relatively simple regular expression. The $ means only match slashes at the end of the string, so it won't remove the first slash in stackoverflow.com/questions/. The curly braces {} are just delimiters; PHP requires matching characters and the front and back of regular expressions, for some silly reason.


Simplest method:

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

Tags:

Php