Reputation: 1962
If I delete an element inside an element, and if its the last element, then the parent element gets shortened to an empty element.
Example:
Start:
<element><child1>Inner HTML</child1><child2>Inner HTML</child2></element>
I delete Child2 and I get:
<element><child1>Inner HTML</child1></element>
I delete Child1 and I get:
<element/>
Now I want that to not happen or at least a way to detect if the element is empty so I can replace it. How can I do that using DOMDocument in PHP.
Thanks...
Upvotes: 3
Views: 5634
Reputation: 13912
Borrowing from the PHP manual:
count($elem->children());
So, to check if empty:
$isEmpty = count($elem->children()) === 0 ? true : false;
Upvotes: 3
Reputation: 655129
You should be able to determine whether an element is empty by inspecting the length of the list of child nodes:
$isEmpty = $elem->childNodes->length === 0;
Note that not only elements can be child nodes of another element but also CDATA sections, comments, and plain text.
Upvotes: 5