使用PHP5提取链接显示文本和href属性

使用PHP5提取链接显示文本和href属性,php,dom,hyperlink,Php,Dom,Hyperlink,我在网上看到了很多关于如何使用PHP5的DOM函数从HTML中提取URL的例子,但是我需要获得链接文本和链接。如果我使用下面的代码提取链接http://X.com从锚定标记中的href属性,如何获取与之关联的相应“yyyy” $oldSetting = libxml_use_internal_errors( true ); libxml_clear_errors(); DOMDocument()非常慢。试试看,或者相反 $html = new DOMDocument(); $html->

我在网上看到了很多关于如何使用PHP5的DOM函数从HTML中提取URL的例子,但是我需要获得链接文本和链接。如果我使用下面的代码提取链接
http://X.com
从锚定标记
中的
href
属性,如何获取与之关联的相应“yyyy”

$oldSetting = libxml_use_internal_errors( true );

libxml_clear_errors();
DOMDocument()非常慢。试试看,或者相反

$html = new DOMDocument();
$html->loadHtmlFile($location);
$xpath = new DOMXPath($html);
$links = $xpath->query( '//a' );
foreach ( $links as $link )
{
$url_list[$i++] = $link->getAttribute( 'href' ) . "\n";
}
libxml_clear_errors();
libxml_use_internal_errors( $oldSetting );