我有一张表,我希望在td中获得具有类名ng-binding的href和td值(class =" ng-binding")。
我当前的代码正确获取了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>";;
}
}
}
}
?>
答案 0 :(得分:2)
您当前的$div
元素(实际上是<td>
)是一个DOMNote,因此您拥有textContent
属性,在这里您可以拆分该字符串(\r\n
只获得第一行,这就是你要找的东西。)
以下是对您的代码的更新:
foreach ($dom->getElementsByTagName('td') as $div) {
if($div->getattribute('class') == 'ng-binding') {
$divContent = $div->textContent;
$relevantContent = explode("\r\n", $divContent)[0];
foreach($div->getElementsByTagName('a') as $link) {
$downloadurl = $link->getattribute('href');
if ($downloadurl !="javascript:void(0);" & $downloadurl !="") {
$counter++;
echo "{$counter}){$downloadurl} :{$relevantContent}<br>\n";
}
}
}
}
答案 1 :(得分:0)
如果您想获取文字内容,可以使用echo $div->textContent;
,但会输出
1)https://somesite.com/sky/download/123456789/5/this-is-a-sample-video.mp4
:testfolder/30 section/this-is-a-sample-video.mp4 View Download Upload 2)https://somesite.com/sky/download/123456789/5/this-is-a-sample-video2.mp4
:testfolder/29 section/this-is-a-sample-video2.mp4 View Download Upload
使用View Download Upload
,因为这也是td
标记内的文字。
答案 2 :(得分:0)
要获取所需的文字,您需要使用$div->textContent
,但要删除子元素的文字内容,您需要从{{div
中移除td
个孩子1}} node:
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++;
$subdiv = $div->getElementsByTagName('div')->item(0);
$div->removeChild($subdiv);
$td_content = trim($div->textContent);
echo "$counter)$downloadurl: $td_content<br>";
}
}
}
}
此外,如果您&
&&
应为griddata
,则您的姓氏会输入错误。