Re: Regular expression - URL validator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Wagner Garcia Campagner wrote:
Thanks again Jim,

That's what i really need.

I'm testing this function...

If i put a URL like www.example.com, then it works fine and turns it to
http://www.example.com

But if i put a URL like http://www.example.com, then it also put another
header so it turns to http://http://www.example.com

I also tried with the strstr function, but receive the same response.

Thanks in advance,
Wagner.

Give this a shot

<plaintext><?php

function fixurl($url) {
	if ( !preg_match('!^(ftp|http(s)?)://!', $url) ) {
		$url = 'http://'.$url;
	}
	return $url;
}
echo fixurl("www.google.com");
echo "\n";
echo fixurl("ftp://www.google.com";);
echo "\n";
echo fixurl("http://www.google.com";);
echo "\n";
echo fixurl("https://www.google.com";);
?>

--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux