Home arrow PHP arrow PHP URL Validation Functions

PHP URL Validation Functions

Validating URLs is important to form handling and PHP data processing. Currently there are numerous solutions for validating URLs. This article will take a look at some of the most commonly used methods of validating URLs in PHP: the Regex method and the PHP built-in Filter_validate_URL.

TABLE OF CONTENTS:
  1. PHP URL Validation Functions
  2. The Test PHP Script
By: Codex-M
Rating: starstarstarstarstar / 6
March 02, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Bear in mind that both methods have their own strengths and weaknesses. These will be examined thoroughly in this article. The objective of this article is to recommend the best possible way of validating URLs based on available choices and results.

The Validation Functions to be Evaluated

Searching Google for "validating URL PHP" or "PHP URL validation" (without quotes) yields the following six result,s which any developer can use and integrate into their own application:

#1. Source: http://php.net/manual/en/function.preg-match.php 

Validating Function:

<?php
function validateURL($url){
$regex = "((https?|ftp)://)?";  
$regex .= "([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?"; 
$regex .= "([a-z0-9-.]*).([a-z]{2,3})"; 
$regex .= "(:[0-9]{2,5})?";  
$regex .= "(/([a-z0-9+$_-].?)+)*/?";  
$regex .= "(?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?";  
$regex .= "(#[a-z_.-][a-z0-9+$_.-]*)?";  
if(preg_match("/^$regex$/", $url)) 

echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="blue">Valid URL</font>'; 

else {
echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="red">Invalid URL</font>';
}
}
?>

#2. Source: http://phpcentral.com/208-url-validation-in-php.html

Validating Function:

<?php
function validateURL($url){
$regex = "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";  
if(preg_match($regex, $url)) 

echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="blue">Valid URL</font>'; 

else {
echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="red">Invalid URL</font>';
}
}
?>

#3. Source:  http://www.blog.highub.com/regular-expression/php-regex-regular-expression/php-regex-validating-a-url/

Validating Function:

<?php
function validateURL($url){
$regex ="/^(([w]+:)?//)?(([dw]|%[a-fA-fd]{2,2})+(:([dw]|%[a-fA-fd]{2,2})+)?@)?([dw][-dw]{0,253}[dw].)+[w]{2,4}(:[d]+)?(/([-+_~.dw]|%[a-fA-fd]{2,2})*)*(?(&amp;?([-+_~.dw]|%[a-fA-fd]{2,2})=?)*)?(#([-+_~.dw]|%[a-fA-fd]{2,2})*)?$/";  
if(preg_match($regex, $url)) 

echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="blue">Valid URL</font>'; 

else {
echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="red">Invalid URL</font>';
}
}
?>

#4. Source: Pyro: http://www.webdeveloper.com/forum/archive/index.php/t-11290.html

Validating Function:

<?php
function validateURL($url){
$regex = "/^(http(s?)://|ftp://{1})((w+.){1,})w{2,}$/i";  
if(preg_match($regex, $url)) 

echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="blue">Valid URL</font>'; 

else {
echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="red">Invalid URL</font>';
}
}
?>

#5. Source: http://php.net/, filter validate URL function: http://php.net/manual/en/filter.filters.validate.php

Validating Function:

<?php
function validateURL($url){
if(filter_var($url, FILTER_VALIDATE_URL))

echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="blue">Valid URL</font>'; 

else {
echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="red">Invalid URL</font>';
}
}
?>

#6. Source: http://stackoverflow.com/questions/206059/php-validation-regex-for-url

<?php
function validateURL($url){
$regex = "#((http|https|ftp)://(S*?.S*?))(s|;|)|]|[|{|}|,|"|'|:|<|$|.s)#ie";  
if(preg_match($regex, $url)) 

echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="blue">Valid URL</font>'; 

else {
echo $url."&nbsp;&nbsp;&nbsp;=".'<font color="red">Invalid URL</font>';
}
}
?>

The URLs to be Tested

Below are the URLs to be tested against the above URL validating functions.

Acceptable URLs (one URL per line):

http://www.example.org
http://www.example.org/
www.example.org/
www.example.org
example.org
subdomain.example.org
https://example.org
https://example.subdomain.org
http://www.example.com/discus/messages/131/24297.html
http://php.net/manual/en/function.preg-match.php
http://www.lgts.com/catalog/product_info.php?products_id=6&osCsid=be3
http://192.168.1.1
http://192.168.1.1/
http://64.233.167.99/
http://siteexplorer.search.yahoo.com/search?p=www.x.com&bwmo=d&bwmf=u
http://www.example.com/2009/01/08/rfc-example-url-validation
http://mp3hungama.com/music/genre_albums.php?id=3
www.enfocus.com/product.php?id=855
http://www.example.com/space%20here.html
http://www.example.com/space here.html
osdir.com/ml/unassigned-bugs/2010-04/msg00162.html
forums.asp.net/p/1157859/1905808.aspx
http://forums.asp.net/p/1157859/1905808.aspx
https://forums.asp.net/p/1157859/1905808.aspx
http://url.com/?source=rss_feed
https://www.sound.com/catalog/account.php?osCsid=07b6922f54ed9674582
https://www.thehayexperts.co.uk/index.php?osCsid=xlo8u8nl8m4t725
ftp://example.com
http://example.com/index.asp
http://www.smallnetbuilder.com/component/option,com_chart/Itemid,189/
http://example.org:80
ftp://asmith@ftp.example.org
HTTP://EN.EXAMPLE.ORG/
HTTP://EXAMPLE.ORG/
HTTP://WWW.EXAMPLE.ORG
http://example.com/redirect?url=http%3A%2F%2Fplanio.com
http://www.example.com:8080
www.linux-rules-the-world.com
http://www.google.com/company_secrets.htm
http://askville.amazon.com/phones/AnswerViewer.do?requestId=7665185

Malformed URLs(one URL per line):

C:forums.asp.net/p/1157859/1905808.aspx
dfds://example.com
http://www.example.com.
http://www.example.com/.
http://.example.com
http://example/
http:///example.com
http://www
htp://www.google.com
http//www.google.com
http://example.com/index.php//
http://example.com//
/newfaq/basic/url.html
http://www.example.commain.html
example.123
http://username:password@hostname/path?arg=value#anchor
.example.
example
http://-example.com
http://example-.com
//example.com.
http://www_google_com
http://www-google-com
http:forums.asp.net/p/1157859/1905808.aspx
http://somedomain.com/ind%ex.html
http://....../path/?query#fragment
http://...../



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: