We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

fris on 11/16/07


Tagged


Versions (?)


Who likes this?

5 people have marked this snippet as a favorite

vali29
sbbath
naz
skywalker
pixelhandler


check for a valid url


Published in: PHP 


  1. function is_valid_url($url) {
  2. $pattern = "#^(http:\/\/|https:\/\/|www\.)(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?(\/)*$#i";
  3. if (!preg_match($pattern, $url)) {
  4. return false;
  5. } else {
  6. return true;
  7. }
  8. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: logankoester on August 20, 2008

This method returns false for valid urls extending beyond the host:

isvalidurl('http://logankoester.com/') => bool(true) isvalidurl('http://logankoester.com/blog') => bool(false)

You need to login to post a comment.