php validate url – Best way to check if a URL is valid

php validate url : check if URL address syntax is valid or not. Validating URL by using FILTER_VALIDATE_URL filter in PHP to check query string and path.

php validate url

how to validate url in php with regex, validate website url, regex, php validation, validation, javascript regex, regular expression.

The simplest and most consistent way to validate a URL in PHP. You can use PHP native function – filter_var to check if the string is normal test or a valid URL.



<!DOCTYPE html>

// Variable to check
$website = "";

// Validate website url
if (filter_var($website, FILTER_VALIDATE_URL)) {
  echo("Enter website URL is a valid URL");
} else {
  echo("Enter website URL is not a valid URL");


Possible flags of validate URL in PHP

FILTER_FLAG_HOST_REQUIRED – URL must include host name (like

FILTER_FLAG_PATH_REQUIRED – URL must have a path after the domain name (like

Example : Validate URL With Regex


$final_validation = "((https?|ftp)\:\/\/)?";
$final_validation .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)[email protected])?";
$final_validation .= "([a-z0-9-.]*)\.([a-z]{2,3})";
$final_validation .= "(\:[0-9]{2,5})?";
$final_validation .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
$final_validation .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?";
$final_validation .= "(#[a-z_.-][a-z0-9+\$_.-]*)?";

$url = '';

if (preg_match("/^$final_validation$/i", $url)) {
   echo('Good Luck, Enter URL is a valid URL');

Don’t Miss : PHP get current URL path

I hope you get an idea about php validate url.
I would like to have feedback on my
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.