[英]How to get content within a A HREF tag using wildcard (PHP)
Im trying to get content of a series of A HREF tags in PHP, eg:我试图在 PHP 中获取一系列 A HREF 标签的内容,例如:
<a href="/user/200890"></a>
<a href="/user/200891"></a>
<a href="/user/200892"></a>
I'm using this, but don't know how to get all tags using wildcard - I tried this, bu didn't work:我正在使用这个,但不知道如何使用通配符获取所有标签 - 我试过这个,但没有工作:
$anchorTags = $xPath->evaluate("//a[@href*=\"/user\"]");
Thanks!谢谢!
You can use the start-with
selector in your XPath query您可以在 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>';
}
This outputs, as expected:正如预期的那样输出:
/user/200890
/user/200891
/user/200892
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.