[英]Manipulate and remove a DOM element from parsed HTML
I'm new to both HTML & PHP. 我是HTML和PHP的新手。 I'm attempting to remove multiple DOM elements from a parsed HTML string. 我正在尝试从已解析的HTML字符串中删除多个DOM元素。
For example: 例如:
<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>
Thanks in advance! 提前致谢!
DOMDocument
is much better for dealing with DOM manipulation ( SimpleXML
is good for just parsing): 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);
}
Very simply if I were to remove lots of things from a string in PHP I would create an array of the parts to remove, loop through them and remove them one at a time from the big string. 非常简单,如果我要从PHP的字符串中删除很多东西,我将创建一系列要删除的部分,循环遍历这些部分,然后一次从大字符串中删除它们。
Like 喜欢
$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);
}
For more information on str_replace see http://php.net/manual/en/function.str-replace.php 有关str_replace的更多信息,请参见http://php.net/manual/zh/function.str-replace.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.