[英]PHP str_ireplace does not remove content from DomDocument tags
I am trying to remove all content from the td
tag having the class
attribute by serving as stripos
to check if the td
indeed has the class
attribute and from str_ireplace
to replace this navigation-only
CSS content class ( <td class="navigation-only"> ... </td>
) with empty double quotes as you can see in my Code below:我正在尝试通过充当stripos
来检查td
是否确实具有class
属性并从td
中str_ireplace
具有class
属性的所有内容,以替换此navigation-only
CSS 内容类( <td class="navigation-only"> ... </td>
) 带有空双引号,如下面的代码所示:
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile("https://website.ndd");
$getTableTags = $doc->getElementsByTagName("table");
$getTdTags = $doc->getElementsByTagName("td");
foreach ($getTableTags as $getTableTag) {
if (stripos($getTableTag->getAttribute('class'), "infotec") !== false) {
foreach ($getTdTags as $getTdTag) {
if (stripos($getTdTag->getAttribute('class'), "navigation-only") !== false) {
// var_dump($getTableTag);
// $completeInfoxbox = $doc->saveHTML($getTableTag);
$getInfoboxPatch1 = str_ireplace($getTdTag, "", $getTableTag);
echo $doc->saveHTML($getInfoboxPatch1);
}
}
}
}
I get a 100% blank Page yet I did an: echo $doc->saveHTML($getInfoboxPatch1)
.我得到一个 100% 的空白页面,但我做了一个: echo $doc->saveHTML($getInfoboxPatch1)
。
So, how can I correct my code so as to REMOVE all the <td>
HTML tags having the class
attribute whose content of this class
attribute is navigation-only
: <td class=" navigation-only"> ... </td>
???那么,如何更正我的代码以删除所有具有class
属性的<td>
HTML 标记,其class
属性的内容是navigation-only
: <td class=" navigation-only"> ... </td>
???
Please help me please.请帮帮我。
$getInfoboxPatch1 = str_ireplace($getTdTag, "", $getTableTag);
// throws error: str_ireplace(): Argument #1 ($search) must be of type array|string, DOMElement given
str_ireplace
takes strings or arrays as search and object parameters. str_ireplace
将字符串或数组作为搜索和对象参数。 You're passing PHP DOMElements
.您正在传递PHP DOMElements
。
To change a PHP DOMDocument element you need to remove the existing element, create a new one, and append it to the old element's parent.要更改 PHP DOMDocument 元素,您需要删除现有元素,创建一个新元素,并将其附加到旧元素的父元素。
In this case you are removing a text node, creating a new text node, and appending the new text node to the TD tag:在这种情况下,您将删除一个文本节点,创建一个新文本节点,并将新文本节点附加到 TD 标记:
if (stripos($getTdTag->getAttribute('class'), "navigation-only") !== false) {
// remove child nodes
while ( $getTdTag->hasChildNodes() ) {
$getTdTag->removeChild($getTdTag->firstChild);
}
// create empty text node
$emptyTextNode = $doc->createTextNode("");
// append to TD tag
$getTdTag->appendChild($emptyTextNode);
//show updated tag
echo $doc->saveHTML($getTdTag);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.