Home > Article > Backend Development > In PHP, the FILTER_VALIDATE_URL constant represents the filter used to validate URLs
The FILTER_VALIDATE_URL constant is used to validate the URL.
FILTER_FLAG_SCHEME_REQUIRED − URL must comply with RFC standards.
FILTER_FLAG_HOST_REQUIRED − The URL must contain the host name.
FILTER_FLAG_PATH_REQUIRED −URL must have a path after the domain name.
FILTER_FLAG_QUERY_REQUIRED −URL must have a query string.
The FILTER_VALIDATE_URL constant does not return anything.
Demonstration
<?php $url = "https://www.example.com"; if (filter_var($url, FILTER_VALIDATE_URL)) { echo("Valid URL!"); } else { echo("Invalid URL!"); } ?>
The following is the output result.
Valid URL!
Let’s look at an example.
Real-time demonstration
<?php $url = "examplecom"; if (filter_var($url, FILTER_VALIDATE_URL)) { echo("Valid URL!"); } else { echo("Invalid URL!"); } ?>
This is the output.
Invalid URL!
The above is the detailed content of In PHP, the FILTER_VALIDATE_URL constant represents the filter used to validate URLs. For more information, please follow other related articles on the PHP Chinese website!