PHP strip img tags from html returning html and images in an array

I need to write a function that accepts some HTML, cuts out img tags and returns html (sans images). However, I also need to save imgs (in an array) so that I can output them to the page separately.

I don't know any php, so what is the best way to do this?

+3
source share
3 answers

You need to familiarize yourself with the DOMDocument class . The best way to do this is to parse the HTML using DOMDocumentand find all tags <img>using getElementsByTagName('img'). If these are the attributes of the images srcyou need, DOMDocument can return them and store them 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);
}
+6
source

, . .

+1
<?php
$pattern = '/<img[^>]*src="([^"]*)[^>]*>/i';
preg_match_all($pattern, $data, $matches);

// image src array
$images = $matches[1];

// no images
$html = preg_replace($pattern, '', $data);
?>
0

All Articles