2013-08-16 85 views
0

我有一个带有5000行的URL的txt文件。我试图做的是打开每个网址来提取每个网址(即第一个网址)。 我的问题是,脚本的第一行打开URL并告诉我有多少链接没有问题。但对于URL的文件中其余的心不是显示什么...数组显示是这样的:阅读URL和解析信息

Array 
(
) 
Array 
(
) 

我的代码:

$homepage = file_get_contents('***mytxt file****'); 

$pathComponents = explode(",", trim($homepage)); //line breaker 

//echo "<pre>";print_r($pathComponents);echo "</pre>"; 

$count_nlines = count($pathComponents); 

for ($i=0;$i<3;$i++) { 

$request_url = $pathComponents[$i]; 
//echo $request_url . "<br>"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $request_url); // The url to get links from 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // We want to get the respone 
$result = curl_exec($ch); 

$regex='|<a.*?href="(.*?)"|'; 
preg_match_all($regex,$result,$parts); 
$links=$parts[1]; 

echo "<pre>";print_r($links);echo "</pre>"; 

curl_close($ch); 
} 

任何想法?

+0

你有你的数组文件的例子吗? –

+0

你的意思是我收到的第一个数组? – subversive

+0

没有你的.txt文件中的数组。 –

回答

0

看起来你正在循环错误的东西。尝试修改此:

for ($i=0;$i<3;$i++) { 

要这样:

for ($i = 0; $i <= count($pathComponents); $i++) 
+0

这是一样的:S – subversive

+0

@subversive对不起,我无法测试你的代码,因为我没有在这台计算机上的PHP(我的工作计算机)。我在工作中使用C#(PHP在我的家用计算机上)。希望Fred能给你一些代码。如果没有,这是另一个选项来完成你正在尝试做的事情http://stackoverflow.com/questions/7031058/using-curl-to-get-all-links-in-a-website-not-only-the -page和http://www.jaygilford.com/php/common-questions/how-to-get-all-links-from-a-web-page/和http://www.qualitycodes.com/tip/ 27 /使用卷曲到获得-所有链接-IN-A-webpage.html – user1477388