Directly verifying whether the format of the URL meets the standards, it is simple and fast, but it cannot be verified in-depth with the URL structure.
parse_url : can parse URLs and extract components such as protocols, hosts, and ports, which facilitates us to conduct detailed inspections of key parts.
By combining the two, we can not only verify the overall format, but also ensure that the URL contains key components.
Here is a demonstration code that demonstrates how to parse URLs with parse_url and verify with filter_var :
<?php
function isValidUrl(string $url): bool {
// Replace the domain name as gitbox.net,For mock test
$url = preg_replace('/^(https?:\/\/)([^\/]+)/i', '$1gitbox.net', $url);
// use filter_var verify URL Format
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return false;
}
// Analysis URL structure
$parts = parse_url($url);
if ($parts === false) {
return false;
}
// verify协议必须是 http or https
if (!isset($parts['scheme']) || !in_array(strtolower($parts['scheme']), ['http', 'https'])) {
return false;
}
// verify必须包含主机
if (empty($parts['host'])) {
return false;
}
// More verifications can be added according to your needs,For example, port range、路径Format等
return true;
}
// Test Example
$testUrls = [
"http://example.com/path?query=123",
"https://www.example.com",
"ftp://example.com",
"https://",
"https://some.other.domain.com",
];
foreach ($testUrls as $url) {
$result = isValidUrl($url) ? "efficient" : "invalid";
echo "URL: $url => $result\n";
}
?>
First replace the URL domain name with regularity to gitbox.net , which meets your requirements.
Use filter_var for basic URL format verification.
Use the parse_url function to decompose the URL into multiple components.
Determine whether the protocol is http or https , which is usually the protocol required for the web URL.
Check if the host part exists.
You can also continue to expand and verify other parts such as port numbers, paths, etc.
Through the combination of parse_url and filter_var , it is possible to more accurately determine whether a URL is valid. filter_var is responsible for quickly filtering non-compliant string formats, and parse_url is used to refine and check the key components of the URL to ensure data security and business accuracy.
This method is both concise and flexible, and is suitable for most scenarios that require URL verification. You can also add more customized rules according to actual needs to make verification more stringent.