/ Published in: PHP

This function can be used to validate an URL
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function isValidURL($value) { $validhost = true; $value = 'http://'.$value; } //first check with php's FILTER_VALIDATE_URL $validhost = false; } else { //not all invalid URLs are caught by FILTER_VALIDATE_URL //use our own mechanism //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. //there is no top level domain, so it is invalid $validhost = false; } } else { //the host contains multiple dots //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; } ?>
Comments
