You want to use something like this:
$string = 'www.blah.com'; $temp_string = (!preg_match('#^(ht|f)tps?://#', $string)) // check if protocol not present ? 'http://' . $string // temporarily add one : $string; // use current if (filter_var($temp_string, FILTER_VALIDATE_URL)) { echo 'is valid'; } else { echo 'not valid'; }
This uses a PHP build to validate URLs. First, it will be checked whether the protocol is present, if it will not temporarily add it to the string being checked, and then run it by checking. This is accurate, unlike the current answer.
source share