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.

0

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Michael. That was what I needed. I had tried saveXML but I was using it incorrectly.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.