When a URL is submitted from a form input by the user, it is very important to check this URL is valid or not before taking any action on it. Here we’ll provide the simple PHP code to validate a URL in PHP. Using filter_var()
function with FILTER_VALIDATE_URL
Filter, we can easily validates a URL in PHP.
Use the following code to check if the variable ($url
) is a valid URL.
<?php
$url = "http://www.codexworld.com";
if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
echo("$url is a valid URL");
} else {
echo("$url is not a valid URL");
}
?>
how can i use it to validated url with file extension?
The post was quite helpful, thank you very much
It works but allows url like http://localhost, is there any way to prevent this?
Thanks for sharing your thoughts on php valid url. Regards