Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

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:
<?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";
        }
?> 

Open in new window

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gary
This seems to work didn't do much testing;

<?php

function checkurl($url){

return(preg_match("#^(https?://)?[^/.]+(\.[^/.]+)+/?$#i",$url));
}

echo "http://www.example.com: " . checkurl("http://www.example.com")."<br>";
echo "http://www.example.com/index.php: " . checkurl("http://www.example.com/index.php")."<br>";
echo "http://www.example.com/friendlyurl/: " . checkurl("http://www.example.com/friendlyurl/")."<br>";
echo "http://example.com: " . checkurl("http://example.com")."<br>";

Open in new window

Avatar of Pedro Chagas

ASKER

Hi @Gary,
I increase this line in your code:
echo "http://example: " . checkurl("http://example.com")."<br>";
and the output is:
http://www.example.com: 1
http://www.example.com/index.php: 0
http://www.example.com/friendlyurl/: 0
http://example.com: 1
http://example: 1
The number 5 should be "0", and 2 and 3 "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("#^(https?://)?([\da-zA-Z\.-]+)\.([a-z\.]{2,6})([\da-zA-Z/\.-]*)*/?$#i",$url));
Hi @Garry,
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: 0
In line above the return is "0", not validate.

Thanks.

~JC
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Check that

return(preg_match("#^(https?://)?([\da-zA-Z_\.-]+)\.([a-z\.])([\d\w/\.=\\?]*)*/?$#i",$url));
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
http://example.games/jjj.php?uu=kjkjk
and validated and well.
But for example:
http://example.c/jjj.php?uu=kjkjk

Open in new window

, 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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
... 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/
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