If you were to actually rewrite that snippet, this is what it would have to look like:
Code: Select all
if (strpos($URL, '/')+1 == strlen($URL)) {
$URL = substr($url, 0, strlen($url)-1);
}
That says: Look for a / in the string URL. Find its position in the string URL. Add 1. If that value is equal to the total length of URL, then there's a / on the end and we need to find the part of URL that starts at position 0 and goes until one character before the end of URL.
It sounds reasonable enough, but the line of thinking is actually absurd. You need to look for the
last / in URL, not the first. So yes this would work fine for things like google.com/ but it wouldnt work for
http://google.com/ or google.com/search/
What you need to do is:
Code: Select all
$urlarry = str_split($url);
foreach ($urlarry as $key => $val) {
if ($val == "e;/"e;) $lastslash = $key;
}
if ($lastslash+1 == strlen($URL))
$URL = substr($url, 0, strlen($url)-1);
But beware if you're using trim() on a relative url... it will not only strip the "/" off the end of the string, but the "/" at the beginning as well. So this code might actually be a good solution.
No wait, it's not. Just use rtrim!
