【发布时间】:2018-03-16 11:48:34
【问题描述】:
我在变量 php .. $html 中有一些 html 内容
我想添加一个类(myClass)只在'img'包括在所有'table'中(不在外面...)
<?php
$html = <<aaa
<img src="..." alt="..." />
<table class="..">
<tr>
<td><img src="..." alt="..." /></td>
</tr>
<!-- more rows ... -->
</table>
<!-- other tables with img -->
aaa;
$dom = new DOMDocument();
$dom->loadHTML($html);
$table = $dom->getElementsByTagName('table');
/*
here, I have to find the table_child <img> and add the class myClass Es. <img class="myClass" .. />
*/
/*
store the new content in a new variabile $html_edit
*/
print "$html_edit";
/*
like this:
$html_edit = <<aaa
<img src="..." alt="..." />
<table class="..">
<tr>
<td><img class="myClass" src="..." alt="..." /></td>
</tr>
<!-- more rows ... -->
</table>
<!-- other tables with <img class="myClass" -->
aaa;
*/
?>
我该怎么做?
【问题讨论】:
-
不是重复的,我只想在 'table' 的 'img' 子项中添加类,而不是在 html 中的所有 'img' 中添加类
-
首先选择表格标签,然后使用该答案申请类。