使用php从url检索主机名无效
我正在尝试解析批量URL。为此,我编写了以下代码。它没有正确解析URL。我确实需要你的建议,我可能需要更改/更改什么才能使代码正常工作。多谢各位使用php从url检索主机名无效,php,Php,我正在尝试解析批量URL。为此,我编写了以下代码。它没有正确解析URL。我确实需要你的建议,我可能需要更改/更改什么才能使代码正常工作。多谢各位 $expr = '[^https*://([^:/]+):?(\d*)/([^?]*)\??(.*)]'; $matches = array(); $fh = fopen('urls.txt', 'r') or die($php_errormsg); while (!feof($fh)) { $item = fgets($fh); if
$expr = '[^https*://([^:/]+):?(\d*)/([^?]*)\??(.*)]';
$matches = array();
$fh = fopen('urls.txt', 'r') or die($php_errormsg);
while (!feof($fh)) {
$item = fgets($fh);
if (preg_match($expr, $item, $matches)) {
list($host, $port, $dir, $args) = array_splice($matches, 1, 4);
print "Host=>$host\n";
print "Port=>$port\n";
print "Dir=>$dir\n";
print "Arguments=>$args\n";
} else {
print "Doesn't match.\n";
}
}
fclose($fh);
使用解析url
$matches = array();
$fh = fopen('urls.txt', 'r') or die($php_errormsg);
while (!feof($fh)) {
$item = fgets($fh);
$tmp = parse_url($item);
if( isset($tmp['host']) ) {
print "Host=".$tmp['host']."\n";
print "Port=".$tmp['port']."\n";
print "Dir=".$tmp['path']."\n";
print "Host=".$tmp['query']."\n";
} else {
print "Doesn't match.\n";
}
}
fclose($fh);
它“不正确”在做什么?也许您想使用parse_url()函数而不是正则表达式?