Revision: 42958
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 14, 2011 07:34 by peckham
Initial Code
<?php function isValidURL($value) { $value = trim($value); $validhost = true; if (strpos($value, 'http://') === false && strpos($value, 'https://') === false) { $value = 'http://'.$value; } //first check with php's FILTER_VALIDATE_URL if (filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === false) { $validhost = false; } else { //not all invalid URLs are caught by FILTER_VALIDATE_URL //use our own mechanism $host = parse_url($value, PHP_URL_HOST); $dotcount = substr_count($host, '.'); //the host should contain at least one dot if ($dotcount > 0) { //if the host contains one dot if ($dotcount == 1) { //and it start with www. if (strpos($host, 'www.') === 0) { //there is no top level domain, so it is invalid $validhost = false; } } else { //the host contains multiple dots if (strpos($host, '..') !== false) { //dots can't be next to each other, so it is invalid $validhost = false; } } } else { //no dots, so it is invalid $validhost = false; } } //return false if host is invalid //otherwise return true return $validhost; } ?>
Initial URL
Initial Description
This function can be used to validate an URL
Initial Title
PHP URL validation
Initial Tags
url, php, validation
Initial Language
PHP