【发布时间】:2017-05-01 18:13:36
【问题描述】:
我有一个表,我想在类名为 ng-binding(class="ng-binding") 的 td 中获取 href 和 td 值。
我当前的代码正确获取了 href 值,但我也想获取相应的 td 值(testfolder/30 section/this-is-a-sample-video.mp4 AND testfolder/29 section/this- is-a-sample-video2.mp4)。例如我想要以下数据:
1)https://somesite.com/sky/download/123456789/5/this-is-a-sample-video.mp4 :testfolder/30 section/this-is-a-sample-video.mp4
2)https://somesite.com/sky/download/123456789/5/this-is-a-sample-video2.mp4 :testfolder/29 section/this-is-a-sample-video2.mp4
如果专家告诉我如何在 td 前面以粗体显示 href 和值,我将不胜感激。提前致谢。
<?php
$html = '<table class="table no-first-border" style="margin-bottom: 0px; table-layout: fixed;">
<tbody><!-- ngRepeat: item in itemContent -->
<tr class="ng-scope" ng-repeat="item in itemContent">
<td class="ng-binding" style="word-wrap: break-word;">testfolder/30 section/this-is-a-sample-video.mp4
<div class="visible-xs" style="padding-top: 10px;">
<!-- ngIf: item.isViewable --><a ng-if="item.isViewable" class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
<a href="https://somesite.com/sky/download/123456789/5/this-is-a-sample-video.mp4" class="btn btn-default" ng-href="https://somesite.com/sky/download/123456789/5/this-is-a-sample-video.mp4" target="_blank">Download</a>
<a class="btn btn-default" href="javascript:void(0);" ng-click="item.uploadItem()" 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 --><a ng-if="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/sky/download/123456789/5/this-is-a-sample-video.mp4" class="btn btn-default" ng-href="https://somesite.com/sky/download/123456789/5/this-is-a-sample-video.mp4" target="_blank">Download</a>
<a class="btn btn-default" href="javascript:void(0);" ng-click="item.uploadItem()" target="_blank">Upload</a>
</td>
</tr><!-- end ngRepeat: item in itemContent -->
<tr class="ng-scope" ng-repeat="item in itemContent">
<td class="ng-binding" style="word-wrap: break-word;">testfolder/29 section/this-is-a-sample-video2.mp4
<div class="visible-xs" style="padding-top: 10px;">
<!-- ngIf: item.isViewable --><a ng-if="item.isViewable" class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
<a href="https://somesite.com/sky/download/123456789/5/this-is-a-sample-video2.mp4" class="btn btn-default" ng-href="https://somesite.com/sky/download/123456789/5/this-is-a-sample-video2.mp4" target="_blank">Download</a>
<a class="btn btn-default" href="javascript:void(0);" ng-click="item.uploadItem()" 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 --><a ng-if="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/sky/download/123456789/5/this-is-a-sample-video2.mp4" class="btn btn-default" ng-href="https://somesite.com/sky/download/123456789/5/this-is-a-sample-video2.mp4" target="_blank">Download</a>
<a class="btn btn-default" href="javascript:void(0);" ng-click="item.uploadItem()" target="_blank">Upload</a>
</td>
</tr><!-- end ngRepeat: item in itemContent -->
</tbody></table>';
$counter=0;
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
foreach ($dom->getElementsByTagName('td') as $div) {
if($div->getattribute('class') == 'ng-binding') {
foreach($div->getElementsByTagName('a') as $link) {
$downloadurl= $link->getattribute('href');
if ($downloadurl !="javascript:void(0);" & $downloadurl !="")
{
$counter++;
echo $counter.")".$downloadurl."<br>";;
}
}
}
}
?>
【问题讨论】:
-
您能否更改 HTML 以使您想要获取的文本位于自己的
<span>或<div>中?这样可以更轻松地提取<td>中的所有其他文本
标签: php parsing dom html-table