I need to write a function that takes some HTML, strips out the img tags and returns the html (sans images). However, I also need to retain the imgs (in an array) so I can output them to the page separately.
I barely know any php so what is the best way to do this?
You'll need to familiarize yourself with the DOMDocument class. The best way to do this is parse out the HTML using DOMDocument, and locate all the <img> tags using getElementsByTagName('img'). If it's the images' src attributes you're after, DOMDocument can return those and store in an array.
// HTML already parsed into $dom
$imgs = $dom->getElementsByTagName('img');
$img_src = array();
// Array of nodes to remove.
$to_remove = array();
foreach ($imgs as $img) {
// Store the img src
$img_src[] = $img->getAttribute('src');
// Delete the node (I think this works)
$to_remove[] = $img;
}
// Then remove all the nodes slated for deletion:
foreach ($to_remove as $node) {
$dom->removeChild($img);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With