RE: [PHP] Regular expression - URL validator
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. -Original Message- From: Jim Lucas [mailto:[EMAIL PROTECTED] Sent: terça-feira, 28 de agosto de 2007 18:35 To: [EMAIL PROTECTED] Cc: PHP General Subject: Re: [PHP] Regular expression - URL validator Wagner Garcia Campagner wrote: Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. You could always do a string comparison for http(s)? in the url if ( strpos($url, array('https://', 'http://')) === false ) { $url = 'http://'.$url; } -- 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
Re: [PHP] Regular expression - URL validator
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
RE: [PHP] Regular expression - URL validator
Yes, make sure you have http:// on the front. ?php if (strtolower(substr($link, 0, 7)) != 'http://') $link = http://$link;; ? On Tue, August 28, 2007 3:22 pm, Wagner Garcia Campagner wrote: Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. -Original Message- From: Jim Lucas [mailto:[EMAIL PROTECTED] Sent: segunda-feira, 27 de agosto de 2007 17:36 To: PHP General; [EMAIL PROTECTED] Subject: Re: [PHP] Regular expression - URL validator Wagner Garcia Campagner wrote: Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); This should be preg_match('/.../i', $_POST['website']) your regex should look something like this. ^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z 0-9.,;\?|\'+%\$#=~_-]+)*$ So, put it all together and it should look like this. ?php $url = ...PUT YOUR TEST URL HERE...; if ( preg_match('!^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1, 5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$!i', $url) ) { echo Matched; } else { echo Did not match; } if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- 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 -- Please vote for this great band: http://acl.mp3.com/feature/soundandjury/?band=COMPANY-OF-THIEVES Requires email confirmation. One vote per day per email limit. Obvious ballot-stuffing will be revoked. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular expression - URL validator
Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. -Original Message- From: Jim Lucas [mailto:[EMAIL PROTECTED] Sent: segunda-feira, 27 de agosto de 2007 17:36 To: PHP General; [EMAIL PROTECTED] Subject: Re: [PHP] Regular expression - URL validator Wagner Garcia Campagner wrote: Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); This should be preg_match('/.../i', $_POST['website']) your regex should look something like this. ^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z 0-9.,;\?|\'+%\$#=~_-]+)*$ So, put it all together and it should look like this. ?php $url = ...PUT YOUR TEST URL HERE...; if ( preg_match('!^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1, 5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$!i', $url) ) { echo Matched; } else { echo Did not match; } if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- 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
Re: [PHP] Regular expression - URL validator
On 8/28/07, Wagner Garcia Campagner [EMAIL PROTECTED] wrote: Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. -Original Message- From: Jim Lucas [mailto:[EMAIL PROTECTED] Sent: segunda-feira, 27 de agosto de 2007 17:36 To: PHP General; [EMAIL PROTECTED] Subject: Re: [PHP] Regular expression - URL validator Wagner Garcia Campagner wrote: Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); This should be preg_match('/.../i', $_POST['website']) your regex should look something like this. ^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z 0-9.,;\?|\'+%\$#=~_-]+)*$ So, put it all together and it should look like this. ?php $url = ...PUT YOUR TEST URL HERE...; if ( preg_match('!^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1, 5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$!i', $url) ) { echo Matched; } else { echo Did not match; } if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- 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 you must use http://www.aol.com not www.aol.com. coz the later is not a valid url. The protocol is not specified there. and if you use www.aol.comis your href of a tags, the browser will automatically add your current web address as prefix as if its a relative url. if your site is http://www.example.com/folder/site.html and if you use href=/www.aol.com it will show http://www.example.com/ www.aol.com if you use href=www.aol.com it will show http://www.example.com/folder/http://www.aol.com/ www.aol.com you have to use href=http://www.aol.com; the absolute one. -- shout at http://shiplu.awardspace.com/ Available for Hire/Contract/Full Time
Re: [PHP] Regular expression - URL validator
Wagner Garcia Campagner wrote: Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. You could always do a string comparison for http(s)? in the url if ( strpos($url, array('https://', 'http://')) === false ) { $url = 'http://'.$url; } -- 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
[PHP] Regular expression - URL validator
Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression - URL validator
Wagner Garcia Campagner wrote: Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); This should be preg_match('/.../i', $_POST['website']) your regex should look something like this. ^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$ So, put it all together and it should look like this. ?php $url = ...PUT YOUR TEST URL HERE...; if ( preg_match('!^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$!i', $url) ) { echo Matched; } else { echo Did not match; } if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- 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