How to validate a URL in PHP


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($urlFILTER_VALIDATE_URL) === false) {
    echo(
"$url is a valid URL");
} else {
    echo(
"$url is not a valid URL");
}
?>

Leave a reply

Connect With CodexWorld