Page 1 of 1
ASP guy's interpretation of a PHP snippet
Posted: Mon Jun 13, 2005 8:04 am
by brickwall
Hello guys, Iam new here and would like to ask for your assistance regarding the code snippet below.
Iam an ASP guy and just beginning to learn PHP. Here is the snippet I want to interpret:
Code: Select all
if (strrpos($URL, '/')+1!=strlen($URL)) {
$URL.='/';
}
Does this mean:
If the URL does not end with a "/", put a "/" at the end of the URL string?
If I want to edit this snippet so that a "/" is not appended in any case, how do I re-write this snippet correctly?
Thanks for your help guys.
Posted: Mon Jun 13, 2005 8:27 am
by Chris Corbyn
It would mean what you think if the "=" was a "==" since "=" is an assignment operator ("==" is for comparison).
If you don't want to append '/' then I guess you just want to take out the
$URL .= "/";
part and put whatever other code in there

Posted: Mon Jun 13, 2005 8:32 am
by brickwall
Thank you very much.
So what does the actual snippet mean then?
If I want to replace the
$URL.='/'
with a "trimmed" version of $URL ("trimmed" meaning without leading and trailing empty spaces), how do I write this?
Sorry for the simplicity of my questions, Iam used to VBScript and this is the first time Iam trying PHP

Posted: Mon Jun 13, 2005 8:52 am
by Weirdan
Posted: Mon Jun 13, 2005 9:04 am
by brickwall
Thanks guys.
I think this solves my simple problem.
Good luck to you all.
Posted: Mon Jun 13, 2005 5:55 pm
by froth
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!

Posted: Tue Jun 14, 2005 2:51 am
by Weirdan
froth wrote:
It sounds reasonable enough, but the line of thinking is actually absurd. You need to look for the last / in URL, not the first.
That's why there was
strrpos in the snippet. Notice the double "r".