Pedro Chagas
asked on
php validate URL
Hi E´s,
I need to validate a URL in PHP.
I search in Internet for try get solution for the problem and I don't see any good script that work perfect.
The most perfect that I found was this code:
Also not validate for this kind of URL's:
http://www.example.com/index.php
http://www.example.com/friendlyurl/
Any idea to improve the regular expression or other way to validate the URL?
The best regards, JC
I need to validate a URL in PHP.
I search in Internet for try get solution for the problem and I don't see any good script that work perfect.
The most perfect that I found was this code:
<?php
$url = "http://www.example.com/index2.php";
if (!preg_match("#^http://www\.[a-z0-9-_.]+\.[a-z]{2,4}$#i",$url)) {
echo "wrong url";
} else {
echo "ok";
}
?>
The code above work partial fine for the domain names, like http://www.example.com validate, but http://example.com (without www) not validate!Also not validate for this kind of URL's:
http://www.example.com/index.php
http://www.example.com/friendlyurl/
Any idea to improve the regular expression or other way to validate the URL?
The best regards, JC
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi @Gary,
I increase this line in your code:
Can you improve the RE?
Hi @Ray: I will read the article!
~JC
I increase this line in your code:
echo "http://example: " . checkurl("http://example.com")."<br>";and the output is:
http://www.example.com: 1The number 5 should be "0", and 2 and 3 "1".
http://www.example.com/index.php: 0
http://www.example.com/friendlyurl/: 0
http://example.com: 1
http://example: 1
Can you improve the RE?
Hi @Ray: I will read the article!
~JC
Yes but the url you are passing has .com - if you remove that it doesn't pass.
Maybe I misunderstood I thought you only wanted the domain and nothing else.
Changed the pattern, allowed bad characters in the first attempt, who'd thought a url would so hard to validate.
return(preg_match("#^(http s?://)?([\ da-zA-Z\.- ]+)\.([a-z \.]{2,6})( [\da-zA-Z/ \.-]*)*/?$ #i",$url)) ;
return(preg_match("#^(http
ASKER
Hi @Garry,
Is possible you improve your solution for check also GET variables in the URL, like this one:
Thanks.
~JC
Is possible you improve your solution for check also GET variables in the URL, like this one:
http://example.com/some.php?hhh=10&dddd=20: 0In line above the return is "0", not validate.
Thanks.
~JC
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Check that
return(preg_match("#^(http s?://)?([\ da-zA-Z_\. -]+)\.([a- z\.])([\d\ w/\.=\\?]* )*/?$#i",$ url));
return(preg_match("#^(http
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I forget that kind of domains exist. now domains can be lot's of things.
For example I test this URL based in new or future domains:
But for example:
If not, can you please improve the RE, for not accept domains with one character?
~JC
For example I test this URL based in new or future domains:
http://example.games/jjj.php?uu=kjkjkand validated and well.
But for example:
http://example.c/jjj.php?uu=kjkjk
, the domain is ".c", and the script validate, do not know if it good or bad. It is possible there are domains with only one character?If not, can you please improve the RE, for not accept domains with one character?
~JC
Add it back in but use {2}
There is no extensions less than 2 characters
There is no extensions less than 2 characters
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
... plus I'm heading to the bar!Yes. Whenever the problem can only be solved with REGEX the truth is to be found here. http://xkcd.com/1171/
ASKER
Based on the idea of @Ray, other way to check URL:
<?
$file = 'http://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
echo "não encontrado";
}
else {
echo "encontrado";
}
?>
Open in new window