The Preg_Match PHP function is used to search a string, and return a 1 or 0. If the search was successful a 1 will be returned, and if it was not found a 0 will be returned. Although other variables can be added, it is most simply phrased as: preg_match(search_pattern, your_string).
There are two scripts which validates the format of a url text string and domain. 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.
- First script validates a url. This make use of regular expression pattern matching.
//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!";
}
?>
- Second script validates a domain.
//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!";
}
?>
No comments:
Post a Comment