2015-01-21 129 views
0

如何从表链接,并将其与PHP保存在file.txt的:获取从表PHP链接

<TABLE width="600" border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed"> 
        <TR>     
         <TD width="15"></TD> 
         <TD width="570" valign="top"> 

          <TABLE width="570" border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed"> 
          <TR> 

          <TD width="190" valign="top"> 

           <TABLE width="190" border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed"> 



             <TR height="98"> 
              <TD width="190" align="center" valign="top"><A HREF="http://mylink.com/1" class="opacityit"><IMG SRC="http://mylink.com/1/784.jpg" title="test1" title="test1" BORDER=0 style="cursor:hand" /></A></TD> 
             </TR> 
             <TR height="2"> 
              <TD width="190"></TD> 
             </TR> 

             <TR> 
              <TD width="190" align="center" Class="text6"><A HREF="http://mylink.com/1" class="Link8" title="test1"><h2 style="color:#000"><font size=2>test1</font></h2></A></TD> 
             </TR> 

           </TABLE> 

          </TD> 

       <TABLE width="190" border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed"> 

             <TR height="98"> 
              <TD width="190" align="center" valign="top"><A HREF="http://mylink.com/2" class="opacityit"><IMG SRC="http://mylink.com/2/784.jpg" title="test2" title="test2" BORDER=0 style="cursor:hand" /></A></TD> 
             </TR> 
             <TR height="2"> 
              <TD width="190"></TD> 
             </TR> 

             <TR> 
              <TD width="190" align="center" Class="text6"><A HREF="http://mylink.com/2" class="Link8" title="test2"><h2 style="color:#000"><font size=2>test2</font></h2></A></TD> 
             </TR> 

           </TABLE> 

          </TD> 

$ HTML =的file_get_contents($ urlcontent );

$dom = new DOMDocument(); 
@$dom->loadHTML($html); 

// grab all the on the page 
$xpath = new DOMXPath($dom); 
$hrefs = $xpath->evaluate("/html/body//tr"); 



for ($i = 0; $i < $hrefs->length; $i++) { 
$href = $hrefs->item($i); 
$url = $href->getAttribute('href'); 

echo $url.'<br />'; 
} 

如何从表链接,并将其与PHP保存在file.txt的

我只是想表

+0

http://mattgemmell.com/what-have-you-tried/ – Umair 2015-01-21 11:20:30

+0

有这样做的几种方法。至少告诉我们你卡在哪里。 – TNC 2015-01-21 11:21:29

+0

我无法检索链接代码不工作? – 2015-01-21 11:29:40

回答

0

这会给你在你的HTML所有链接的链接:

$html = file_get_contents($urlcontent); 

$dom = new DOMDocument(); 
@$dom->loadHTML($html); 

$links = array(); 
foreach($dom->getElementsByTagName('a') as $node) 
    $links[] = $node->getAttribute('href'); 

print_r($links); 

,如果你想在表中只有链接:

$html = file_get_contents($urlcontent); 

$dom = new DOMDocument(); 
@$dom->loadHTML($html); 

$links = array(); 
foreach($dom->getElementsByTagName('table') as $table) 
    foreach($table->getElementsByTagName('a') as $node){ 
     $href = $node->getAttribute('href'); 
     if(!in_array($href, $links)) 
      $links[] = $href; 
    } 

print_r($links); 
+0

我只想得到表格的链接 – 2015-01-21 11:42:52

+0

@cleanguyFr我更新了我的答案 – Kiyan 2015-01-21 16:26:16

0

可以小写HTML后应用类似如下的一些代码串

$matches = array(); 
preg_match('/<a\s[^>]*href=\"([^\"]*)\"/', $url, $matches);