Newbie: test for string/ if-elseif

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
daveW
Forum Newbie
Posts: 2
Joined: Fri May 27, 2005 12:30 pm

Newbie: test for string/ if-elseif

Post by daveW »

Hi all
I'm stumbling at the first hurdle here, wondering if anyone can give me a hint?

I'm trying to test for the presence of "http://" in a string then return one of two variables.

Code: Select all

<?php
 $url = "http://www.domain.co.uk";
 $link1 = "<a href=\"";
 $link2 = "<a href=\"http://";

 if ( $url == "^(http://).*" )
 {
 $version = $link1;
 }
 elseif  ( $url != "^(http://).*" )
 {
 $version = $link2;
 }
 
 echo $version,$url,"\">$url</a><br>";
?>
this always returns '$link2'. The problem is in the IF statement but I can't get my head around it.

Any help much appreciated
daveW
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

the way you're heading right now you'd need to use preg_match() (regex). If you're just looking for http:// you could always just use strpos()
daveW
Forum Newbie
Posts: 2
Joined: Fri May 27, 2005 12:30 pm

Post by daveW »

wow, that was a quick reply!

that was just the tip I needed.
while looking at 'strpos' I also found 'strstr' and that works just fine

thanks very much
Post Reply