PHP FILTER_VALIDATE_URL Filter
Complete PHP Filter Reference Manual
Example
Check whether the URL format is valid:
The output of the code above is:
https://www.runoob.com 是正确的 URL
Definition and Usage
The FILTER_VALIDATE_URL filter validates a value as a URL.
- Name: "validate_url"
- ID-number: 273
Possible flags:
- FILTER_FLAG_SCHEME_REQUIRED - Requires the URL to be an RFC-compliant URL (such as http://runoob)
- FILTER_FLAG_HOST_REQUIRED - Requires the URL to include a host name (such as http://www.runoob.com)
- FILTER_FLAG_PATH_REQUIRED - Requires the URL to have a path after the domain name (such as www.runoob.com/example1/test2/)
- FILTER_FLAG_QUERY_REQUIRED - Requires the URL to have a query string (such as "example.php?name=Peter&age=37")
Example
The following example removes illegal characters from the $url variable and then checks whether it is a valid URL:
Example
The output of the code is shown below:
https://www.runoob.com 是正确的 URL
The following checks whether the URL contains a valid query string:
Example
The output of the code is shown below:
https://www.runoob.com 是错误的 URL
https://www.runoob.com?s=php 是正确的 URL
Complete PHP Filter Reference Manual