Regular Expression to Validate URLs

By | February 10, 2010

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. :)