我正在尝试解析批量网址。我写了以下代码来做到这一点。它没有正确解析网址。我确实需要你的建议,我可能需要更改/更改代码才能运行。谢谢。
$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);
答案 0 :(得分:0)
使用parse_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);