Page 1 of 1

Newbie: test for string/ if-elseif

Posted: Fri May 27, 2005 12:46 pm
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

Posted: Fri May 27, 2005 12:50 pm
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()

Posted: Fri May 27, 2005 6:16 pm
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