I am trying to get value of href inside a div with class name(class="visible-xs"). I tried this code and it gets all the href outside div as well which I don't want.如何获得<a href= value inside div with class name only?
$dom = new DOMDocument;
$dom->loadHTML($code2);
foreach ($dom->getElementsByTagName('a') as $node)
{
echo $node->getAttribute("href")."\n";
}
Then i tried following but it gives me error(Fatal error: Call to undefined method DOMDocument::getElementsByClassName() in..):
$dom = new DOMDocument;
$dom->loadHTML($code2);
foreach ($dom->getElementsByClassName('visible-xs') as $bigDiv) {
echo $bigDiv->getAttribute("href")."\n";
}
could any one help me fix the above error and only get the value of href inside div with class name visible-xs ?Thanks in advance.
sample data:
<tr class="ng-scope" ng-repeat="item in itemContent">
<td class="ng-binding" style="word-wrap: break-word;">test/folder/1.mp4
<div class="visible-xs" style="padding-top: 10px;">
<!-- ngIf: item.isViewable --> class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
<a href="https://somesite.com/test/1.mp4" class="btn btn-default" ng-href="https://somesite.com/test/1.mp4" target="_blank">Download</a>
<a class="btn btn-default" href="javascript:void(0);" ng-click="item.upload()" target="_blank">Upload</a>
</div>
</td>
<!-- ngIf: hasViewables --><td class="text-right hidden-xs ng-scope" style="white-space: nowrap; width: 60px;" ng-if="hasViewables">
<!-- ngIf: item.isViewable -->class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
</td><!-- end ngIf: hasViewables -->
<td class="text-right hidden-xs" style="white-space: nowrap; width: 250px;">
<a href="https://somesite.com/test/1.mp4" class="btn btn-default" ng-href="https://somesite.com/test/1.mp4" target="_blank">Download</a>
javascript:void(0);" ng-click="item.upload()" target="_blank">Upload</a>
</td>
</tr>
谢谢你的答复。你能告诉我一个如何使用xpath来达到这个目的的例子吗? – user1788736
我尝试过但没有数据:$ doc = new DOMDocument(); @ $ doc-> loadHTML($ code2); $ xpath = new DomXPath($ doc); echo $ link-> getAttribute('href');} $ foreach($ xpath-> query('// div [class =“visible-xs”] // a [@href]')as $ link){ echo $ link-> getAttribute } – user1788736
DIV类名中可见-XS和 user1788736