2011-06-26 59 views
0

当使用以下代码时,出现“提供的参数无效”。我可以成功解析一个IP地址和端口号,但我不知道如何一次获得多个IP地址和端口号。我的foreach循环不起作用。有任何想法吗?PHP中的foreach循环获取提供的无效参数

$dom = new DOMDocument(); 
$dom->loadHTMLFile($url); 
$xml = simplexml_import_dom($dom); 
$dom_results = $xml->xpath("/html/body/div[@id='subpagebgtabs']/div[@id='container']/table[@id='listtable']"); 

$ip_address = $dom_results[0]->tr->td[1]->span; 
$ip_post = $dom_results[0]->tr->td[2]; 

$address_parts = $ip_address.":".$ip_post; 

foreach ($address_parts as $address_full){ 
    echo $address_full . "<br>"; 
} 

$ Dom_Results输出

["tr"]=> 
    array(50) { 
     [0]=> 
     object(SimpleXMLElement)#5 (3) { 
     ["@attributes"]=> 
     array(2) { 
      ["class"]=> 
      string(0) "" 
      ["rel"]=> 
      string(7) "9054676" 
     } 
     ["comment"]=> 
     object(SimpleXMLElement)#56 (0) { 
     } 
     ["td"]=> 
     array(8) { 
      [0]=> 
      object(SimpleXMLElement)#57 (2) { 
      ["@attributes"]=> 
      array(2) { 
       ["class"]=> 
       string(20) "leftborder timestamp" 
       ["rel"]=> 
       string(10) "1309047901" 
      } 
      ["span"]=> 
      string(10) "2 minutes" 
      } 
      [1]=> 
      object(SimpleXMLElement)#58 (1) { 
      ["span"]=> 
      string(13) "122.72.10.201" 
      } 
      [2]=> 
      string(3) "80" 
+4

'$ address_parts'对我来说看起来不像一个数组。 –

+1

那么我可以看到$ adress_parts不是数组。 – nullpotent

+1

你知道$ address_parts应该是一个数组吗? – leon

回答

0

我认为这是你在找什么:

// If results are found 
if (! empty($dom_results)) 
    // Loop through each result. Based on your XPath query, the $dom_results 
    // contains tables. This loops through the rows of the first table. 
    foreach ($dom_results[0]->tr as $row) 
    { 
    $ip_address = $row->td[1]->span; 
    $ip_post = $row->td[2]; 

    // Output the address 
    echo $ip_address . ":" . $ip_post . "<br />"; 
    } 
+0

我认为这是我需要的,但它只返回页面上的IP地址1。有没有办法让它一次全部返回? – sarsar

+0

@sarsar - 我更新了答案,以循环遍历每个表格行(TR)而不是每个表格。这可能会解决问题。数据如何存储在你想分析的表格中并不是很清楚(现在还不是)。 –

+0

完美!非常感谢。 – sarsar

0

似乎要提取所有的IP地址和端口号并连接它就像

ipaddress:端口

所以试试这个

foreach($dom_results as $dom) { 
    $ip = $dom->tr->td[1]->span; 
    $port = $dom->tr->td[2]; 
    $address = $ip . ":". $port; 
    echo $address . "<br />"; 
} 
相关问题