Home > PHP Solutions, Regexps > Regular Expression to Validate URLs

Regular Expression to Validate URLs

February 10th, 2010 Leave a comment Go to comments

Here is a short regexp that is used to validate whether a user has entered correct URL address. Might be useful in any scripts dealing with user data.

I will show it as PHP code:

preg_match(‘/^(http:\/\/|https:\/\/)([^\.\/]+\.)*([a-zA-Z0-9])([a-zA-Z0-9-]*)\.([a-zA-Z]{2,4})(\/.*)?$/i’, $_POST['url']);

It will check whether url suits the pattern. May not be ideal, but it’s working. :)

  1. No comments yet.
  1. No trackbacks yet.