Regular Expression to validate URL format

Posted by admin
This is a simple Regular Expression to validate if the URL or string is a valid URL format. Please fell free to comment and improve this function. Thanks...!
$string_url = "http://www.iwebprovider.com";

$reg_exp = "/^(http(s?):\/\/)(www\.)?[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/";

if(preg_match($reg_exp, $string_url) == TRUE){

echo "Valid URL Format.";

}
Back to List | Comments (3) | October 03, 2009 at 03:21 pm

3 Responses

ElenaLisvato says:
Aug 06, 2009 at 07:23 pm

Hello Guru, what entice you to post an article. This article was extremely interesting, especially since I was searching for thoughts on this subject last Thursday.

Extenze says:
Aug 13, 2009 at 12:59 pm

Hello Guru, what entice you to post an article. This article was extremely interesting, especially since I was searching for thoughts on this subject last Thursday.

Admin says:
Aug 14, 2009 at 01:35 am

Hello Extenze,



I created this code or regular expression because I want to used it for my site in validating URLs. Example of pages that uses this regular expression are my Blog, News and Contact Us page. So you are currently using it already.



Before I can't find a dynamic but simple tutorial with regular expression for validating URL. Some blogs and tutorials I have found before is to complicated, some are not so dynamic in the sense that some can’t validate URL query string.



Example it can't validate URL query string like index.php?page=blogs&id=1, it can only validate up to http://domain.com/.

So that's why I have decided to create my own regular expression for validating URL with query string.

Leave a Reply

Name (required)

Email (required) will not be publish

Website (optional) ex: http://www.domain.com

Notify me of followup comments via e-mail. (optional)

Your Message (required)

Enter Security Code (required)
captcha  

Latest News (2)

iWebProvider.com No 1 at Yahoo search engine for the keyword -filipino web developer.

Posted on October 02, 2009

On Sept 6, 2009 iWebProvider.com is No. 1 at Yahoo and Google for keyword "filipino web developer".

continue reading

Launching of the updated iWebProvider.com

Posted on October 02, 2009

Now as of July 7, 2009 the launching of iWebProvider.com with its improve feature and the used of its very own CMS. It now has features like Blogs, comments on News articles and the upcoming user registration.

continue reading

Wise Words

"It's not about how many you have learned but how much you have utilized and mastered what you have learned."

- by: Mark Lester
Get subscribers