php

How to check if a URL is valid in PHP

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.
 

How to check if a URL is valid in PHP
<?php
	$url = "https://stackhowto.com";

	if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
		echo("Url is valid");
	} else {
		echo("Url is not valid");
	}
?>

Output:

Url is valid
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *