Re: Regular expression - URL validator
| From: | Jim Lucas | Date: | Wed, 29 Aug 2007 15:55:10 +0000 |
| Subject: | Re: Regular expression - URL validator | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-261460@lists.php.net to get a copy of this message | ||
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