2016-12-15 95 views
0

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> 
+0

谢谢你的答复。你能告诉我一个如何使用xpath来达到这个目的的例子吗? – user1788736

+0

我尝试过但没有数据:$ 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

+0

DIV类名中可见-XS和 user1788736

回答

1

There is no getElementsByClassName function. Iterate over your divs, check the class, if matched pull the links inside and output the hrefs you want (or break if you want to stop after the first match).

$dom = new DOMDocument; 
libxml_use_internal_errors(true); 
$dom->loadHTML($html); 
libxml_clear_errors(); 
foreach ($dom->getElementsByTagName('div') as $div) { 
    if($div->getattribute('class') == 'visible-xs') { 
      foreach($div->getElementsByTagName('a') as $link) { 
       echo $link->getattribute('href'); 
      } 
    } 
} 

Demo: https://eval.in/698484

breakhttps://eval.in/698488为例。

+0

非常感谢。那演示帮助我很多,但我怎么可以忽略Upload的href值?(我的错误,我忘记纠正示例数据) – user1788736

+0

当然你可以使用'strpos'或'preg_match'来阻止'javascript'的href's。同样的方式我检查了课程。 – chris85

相关问题