Daphne
Daphne

Reputation: 1747

Use regex, php or regex in conjunction with php to detect the presence of a trailing slash at the end of the URL?

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

Answers (2)

Jon
Jon

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

m0skit0
m0skit0

Reputation: 25873

/^(.*)\/$/

This should evaluate to true if trailing slah, false otherwise.

Regex group 1 is the URL without the trailing slash.

Upvotes: 0

Related Questions