2

Possible Duplicate:
How can I get an element's serialised HTML with PHP's DOMDocument?
PHP + DOMDocument: outerHTML for element?

I am trying to extract all img tags from a string. I am using:

$domimg = new DOMDocument();
@$domimg->loadHTML($body);
$images_all = $domimg->getElementsByTagName('img');

foreach ($images_all as $image) {
  // do something
}

I want to put the src= values or even the complete img tags into an array or string.

Community
  • 1
  • 1
user191688
  • 2,609
  • 5
  • 26
  • 30

2 Answers2

9

Use saveXML() or saveHTML() on each node to add it to an array:

$img_links = array();
$domimg = new DOMDocument();
$domimg->loadHTML($body);
$images_all = $domimg->getElementsByTagName('img');

foreach ($images_all as $image) {
  // Append the XML or HTML of each to an array
  $img_links[] = $domimg->saveXML($image);
}

print_r($img_links);
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
0

You could try a DOM parser like simplexml_load_string. Take a look at a similar answer I posted here: Needle in haystack with array in PHP

Community
  • 1
  • 1
travega
  • 8,284
  • 16
  • 63
  • 91