PHP URL Validation Script

This script validates the format of a url text string. It might be used to check the url submitted from a form. The format of the url is expected to be of the form: domain-name.co.uk for example.

There are actually 2 scripts. One validates a url and the other does domain name validation. They make use of regular expression pattern matching.

<?php
//url validation
$url = 'teSTgjhgj.co.uk';
if (preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", $url)) {
    print "$url url OK.";
} else {
    print "$url url not valid!";
}
?>

<?php
//domain validation
$domain = 'teSTgjhgj';
if (preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9]$/i", $domain)) {
    print "$domain Domain OK.";
} else {
    print "$domain Domain not valid!";
}
?>
logo
Webmaster Tools & Resources
Blog