ASP guy's interpretation of a PHP snippet

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brickwall
Forum Newbie
Posts: 3
Joined: Mon Jun 13, 2005 7:49 am

ASP guy's interpretation of a PHP snippet

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
brickwall
Forum Newbie
Posts: 3
Joined: Mon Jun 13, 2005 7:49 am

Post 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 :D
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

trim

Code: Select all

$URL = trim($URL);
brickwall
Forum Newbie
Posts: 3
Joined: Mon Jun 13, 2005 7:49 am

Post by brickwall »

Thanks guys.

I think this solves my simple problem.

Good luck to you all.
froth
Forum Commoner
Posts: 31
Joined: Sat Jan 22, 2005 9:26 pm

Post 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 == &quote;/&quote;) $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! :lol:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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".
Post Reply