我正在尝试获取PHP中一系列A HREF标记的内容,例如:
<a href="/user/200890"></a>
<a href="/user/200891"></a>
<a href="/user/200892"></a>
我正在使用此功能,但不知道如何使用通配符获取所有标签-我尝试了此操作,但bu没用:
$anchorTags = $xPath->evaluate("//a[@href*=\"/user\"]");
谢谢!
您可以在XPath查询中使用start-with
选择器
$strhtml='
<a href="/user/200890"></a>
<a href="/user/200891"></a>
<a href="/user/200892"></a>
';
libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->strictErrorChecking=false;
$dom->loadHTML( $strhtml );
$errors=libxml_get_errors();
libxml_clear_errors();
$xp=new DOMXPath( $dom );
$pttn='//a[ starts-with( @href, "/user/" ) ]';
$col=$xp->query( $pttn );
if( $col && $col->length > 0 ){
foreach( $col as $node )echo $node->getAttribute('href') . '<br>';
}
此输出符合预期:
/user/200890
/user/200891
/user/200892