You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What I want to do is "unwrap" each <div> by relocating the <p> child nodes to be direct descendants of the div's parent, and deleting the now-empty <div> node. I tried doing it using the code below, but it only works on some nodes; on others, the $tag element has no parent and therefore you can't use insertSiblingBefore(...) without throwing an exception.
Here's some sample code to call the the desired function:
// Remove <div> tags with no attributes
$divTags = $doc->find( 'div' );
foreach ( $divTags as $divTag ) {
removeElementKeepingChildren( $divTag );
}
...and here's the offending code itself:
function removeElementKeepingChildren( Element $tag ) : void {
// I'd like to avoid needing this check...but without it, certain $tags cause the exception
if ( ! $tag->parent() ) {
return;
}
// "Backup" the children
$children = $tag->children();
// Remove the children from the node to be removed
$tag->removeChildren();
foreach ( $children as $child ) {
$tag->insertSiblingBefore( $child );
}
$tag->remove();
}
There's got to be a better way to do this...suggestions?
The text was updated successfully, but these errors were encountered:
Hi all, I hope it's OK if I ask a question here, I haven't found any other place to ask.
I have the following sample HTML:
What I want to do is "unwrap" each
<div>
by relocating the<p>
child nodes to be direct descendants of the div's parent, and deleting the now-empty<div>
node. I tried doing it using the code below, but it only works on some nodes; on others, the$tag
element has no parent and therefore you can't useinsertSiblingBefore(...)
without throwing an exception.Here's some sample code to call the the desired function:
...and here's the offending code itself:
There's got to be a better way to do this...suggestions?
The text was updated successfully, but these errors were encountered: