Reputation: 1747
I'm trying to use preg_match
in conjunction with regex to detect whether there is a trailing slash at the end of a URL.
If there is a trailing slash at the end of the URL, I'd like to use PHP to delete the trailing slash from the URL.
Upvotes: 1
Views: 166
Reputation: 437376
The most appropriate function for this case is rtrim
:
$url = rtrim($url, '/');
This will actually remove all trailing slashes (and not a maximum of one), but I doubt this is something to be concerned about when talking URLs.
There is no need to use a regex or other heavy-duty tools.
Upvotes: 3
Reputation: 25873
/^(.*)\/$/
This should evaluate to true if trailing slah, false otherwise.
Regex group 1 is the URL without the trailing slash.
Upvotes: 0