HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
PHP Filter Reference : Check if the variable $url is a valid URL
The FILTER_VALIDATE_URL filter validates a URL.
Possible flags:
The example below both sanitizes and validates an URL:
First remove all illegal characters from the $url variable, then check if it is a valid URL:
<?php
$url = "https://w3resource.net";
// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);
// Validate url
if (filter_var($url, FILTER_VALIDATE_URL)) {
echo("$url is a valid URL");
} else {
echo("$url is not a valid URL");
}
?>
Here, the URL is required to have a query string to be valid:
<?php
$url = "https://w3resource.net";
if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED)) {
echo("$url is a valid URL");
} else {
echo("$url is not a valid URL");
}
?>
❮ PHP Filter Reference