38
loading...
This website collects cookies to deliver better user experience
Example : PHP FILTER_VALIDATE_URL Filter
<!DOCTYPE html>
<html>
<body>
<?php
// Variable to check
$url = "https://www.websolutionstuff.com";
// Validate url
if (filter_var($url, FILTER_VALIDATE_URL)) {
echo("Enter URL is a valid URL");
} else {
echo("Enter URL is not a valid URL");
}
?>
</body>
</html>
FILTER_FLAG_HOST_REQUIRED - URL must include host name (like https://www.websolutionstuff.com)
FILTER_FLAG_PATH_REQUIRED - URL must have a path after the domain name (like www.websolutionstuff.com/post)
Example : Validate URL With Regex
<?php
$regex = "((https?|ftp)\:\/\/)?";
$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?";
$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})";
$regex .= "(\:[0-9]{2,5})?";
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?";
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?";
$url = 'https://websolutionstuff.com/';
if (preg_match("/^$regex$/i", $url)) {
echo('Enter URL is a valid URL');
}