我有一个获取正确网址的功能:example.com 到http://example.com, www.example.org 到https://example.org等。
function startsWith($haystack, $needle) {
return !strncmp($haystack, $needle, strlen($needle));
}
function properUrl($url) {
$urls = array();
if (startsWith($url, "https://") || startsWith($url, "http://")) {
$urls[] = $url;
} else if (startsWith($url, "www.")) {
$url = substr($url, 4);
$urls[] = "http://$url";
$urls[] = "http://www.$url";
$urls[] = "https://$url";
$urls[] = "https://www.$url";
} else {
$urls[] = "http://$url";
$urls[] = "http://www.$url";
$urls[] = "https://$url";
$urls[] = "https://www.$url";
}
foreach ($urls as $u) {
if (@file_get_contents($u)) {
$url = $u;
break;
}
}
return $url;
}
什么是更快的算法而不是 file_get_contents。我想要一个正确的网址,而不是阅读整个页面。谢谢。