我是HTML& PHP。我正在尝试从解析的HTML字符串中删除多个DOM元素。
例如:
<tbody>
<tr>
<td>I'd like to find and remove this text</td>
<td>& possibly this too</td>
<td>can you help?</td>
</tr>
</tbody>
提前致谢!
答案 0 :(得分:1)
DOMDocument
对于处理DOM操作要好得多(SimpleXML
只对解析有用):
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$textNodes = $xpath->query('//text()');
foreach ($textNodes as $node) {
$node->parentNode->removeChild($node);
}
答案 1 :(得分:-1)
很简单,如果我要从PHP中的字符串中删除很多东西,我会创建一个要删除的部分数组,循环遍历它们并从大字符串中一次删除它们。
像
$html = "Loads of html blah blah blah blah......";
$array = array(
"Remove me",
"and me",
"remove me too"
);
foreach($array as $string){
str_replace($string, '', $html);
}
有关str_replace的更多信息,请参阅http://php.net/manual/en/function.str-replace.php