2017-06-09 58 views
0

所有的HREF我想提取所有的HREF:摘自一个HTML链接

https://plugins.svn.wordpress.org/

,并添加到foreach循环。

我使用PHP 简单的HTML DOM解析器http://simplehtmldom.sourceforge.net/

,但它只是超时试图.... 任何帮助,将不胜感激。 这里是我的代码:

// Create DOM from URL or file 
$html = file_get_html('https://plugins.svn.wordpress.org/'); 

// Find all links 
foreach($html->find('a') as $element) 
       echo $element->href . '<br>'; 

OR 使用对象定向方式:

// Create a DOM object 
$html = new simple_html_dom(); 

// Load HTML from a URL 
$html->load_file('https://plugins.svn.wordpress.org/'); 

// Find all links 
foreach($html->find('a') as $element) 
       echo $element->href . '<br>'; 
+0

你应该添加你试过的代码 – RamRaider

+0

为什么不使用正则表达式ssion – king

+0

“超时”可能是因为html内容很大 – king

回答

0

解决您的问题,请使用此代码将帮助您更好地

<?php 
$html = file_get_contents('http://niraj140792.wordpress.com/'); 
//Create a new DOM document 
$dom = new DOMDocument; 

@$dom->loadHTML($html); 

$links = $dom->getElementsByTagName('a'); 

foreach ($links as $link){ 
    //Extract and show the "href" attribute. 
    echo $link->nodeValue; 
    echo $link->getAttribute('href'), '<br>'; 
} 
?> 
+0

,并且在问题中是否与给定的url一起工作? – RamRaider

+0

yes ofcourse working just checked ... but why you are downvoting this –

+0

哇@Nirajpatel它的工作原理! :) – samjco