You are here: Home / Topics / How to check if a URL is valid in PHP

How to check if a URL is valid in PHP

Filed under: PHP on 2022-10-30 19:44:19

In this tutorial, we are going to see how to check if a URL is valid in PHP. When a URL is submitted, it is very important to check if this URL is valid or not before performing any action. Here we are going to see how to check if a URL is valid in PHP using filter_var() function with FILTER_VALIDATE_URL filter. Use the following code to check if the URL is valid.

Validate URL in PHP filter_var() function

<?php

$url = "https://google.com";

 

if (!filter_var($url, FILTER_VALIDATE_URL) === false) {

echo("Url is valid");

} else {

echo("Url is not valid");

}

?>

Output of this program will be 

Url is valid.

Practice and Learn PHP mcqs here. PHP MCQ Questions

About Author:
T
Team MCQ Buddy     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.