How to Remove an HTML Element Using the Domdocument Class

How to remove an HTML element using the DOMDocument class

http://us2.php.net/manual/en/domnode.removechild.php

DomDocument is a DomNode.. You can just call remove child and you should be fine.

EDIT: Just noticed you were probably talking about the page you are working with currently. Don't know if DomDocument would work. You may wanna look to use javascript at that point (if its already been served up to the client)

PHP DOMDocument: Delete elements by class

You need to use the removeChild() method of the parent element:

$xpath = new DOMXPath($dom);
foreach($xpath->query('//div[contains(attribute::class, "foo")]') as $e ) {
// Delete this node
$e->parentNode->removeChild($e);
}

Btw, about your second question, if there are no elements found, the loop won't iterate at all.


Here comes a working example:

$html = <<<EOF
<div class="main">
<div class="delete_this" contenteditable="true">Target</div>
<div class="class1"></div>
<div class="content"><p>Anything</p></div>
</div>
EOF;

$doc = new DOMDocument();
$doc->loadHTML($html);

$selector = new DOMXPath($doc);
foreach($selector->query('//div[contains(attribute::class, "delete_this")]') as $e ) {
$e->parentNode->removeChild($e);
}

echo $doc->saveHTML($doc->documentElement);

How to delete element with DOMDocument?

You remove the node by telling the parent node to remove the child:

$href->parentNode->removeChild($href);

See DOMNode::$parentNodeDocs and DOMNode::removeChild()Docs.

See as well:

  • How to remove attributes using PHP DOMDocument?
  • How to remove an HTML element using the DOMDocument class

how to remove a class using dom in php?

DOMDocument has no find() method, you have to use DOMXPath::evaluate() with XPath expressions.

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_use_internal_errors(false);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);

$expression =
'//div[
@id="essay"
]
/section[
contains(
concat(" ", normalize-space(@class), " "), " refs "
)
]';

foreach ($xpath->evaluate($expression) as $section) {
$section->removeAttribute('class');
}
echo $dom->saveHtml();

Class attributes can contain multiple values like classOne classTwo. With normalize-space() the whitespaces will be reduced to single spaces inside the string (start and end removed). concat() add spaces to the start and end. This avoid matching the class name as part of another class name.

In the example the whole class attribute will be removed. To modify it you can read it with DOMElement::getAttribute() and use string functions to change it.

Here are several DOM based libraries that can make HTML manipulation easier.

Remove list item by class name using PHP

You can use DOMXPath in conjunction with DOMDocument. Try the following:

<?php

$html = '<ul>
<li><a href="/first">First Item</a></li>
<li><a class="hide" href="/first">Second Item</a></li>
</ul>';

$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$elements = $xpath->query('//a[@class="hide"]/..');

foreach ($elements as $el) {
$el->parentNode->removeChild($el);
}

echo $doc->saveHTML();

Yields:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">

<html>
<body>
<ul>
<li><a href="/first">First Item</a></li>
</ul>
</body>
</html>

The XPath expression //a[@class="hide"]/.. will match any <a> tags containing the "hide" class, and /.. returns its parent. So you are returning the parent node of any match; i.e: the parent <li> of any match, in your example.

Finally, we iterate over all matching <li> elements and remove each one from its parent node.

Here's more information on DOMXPath::query()

Hope this helps :)

Remove specific class domdocument

The /.. at the end of your XPath selector is selecting the parent element, not the <span> itself - .. means to work one level back up the tree, the same as in a directory path. So in your loop, where you say parentNode->removeChild, you're actually removing the div, since $el is already the span's parent element.

If you just remove the /.. from the end of the selector, the code should work as intended.

$xpath = new DOMXPath($doc);
$elements = $xpath->query('//span[@class="hello"]');

foreach ($elements as $el) {
$el->parentNode->removeChild($el);
}

Full example: https://3v4l.org/o4dRv

PHP DOMDocument : How remove a div?

The removeChild method removes a child element from a node. A div cannot be a child element of the document object, only <html> can be (in an HTML document).

Having found the div you want to remove, you need to fetch its parent node. Then call removeChild on that.

$divMeta->parentNode->removeChild($divMeta)


Related Topics



Leave a reply



Submit