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");
}
?>

4 Comments

  1. Ilori Olaitan Adedapo Said...
  2. Mr Quynh Said...
  3. Laurent Said...
  4. Campervan Hire Adelaide Said...

Leave a reply

keyboard_double_arrow_up