-1

I'm trying to use Simple HTML DOM Parser to look at the content of my WordPress posts and move all images to the end of the post, regardless of where they are in the actual html.

I've successfully isolated all the images using:

$html = str_get_html(wpautop(get_the_content()));
foreach($html->find('img') as $element) echo $element->src . '<br>';

as per the documentation. (This just prints the image sources as per the example given on the site as I'm still experimenting).

However, I couldn't figure out how to find all elements except images - the documentation has options for finding elements without an attribute, but that doesn't seem to apply.

I could select multiple elements like so:

$ret = $html->find('a, p'); 

...but then I'd have either have to guess at which tags were going to be used, or include every tag that isn't an img, which would be a huge and unreliable list. Is there any way around this?

Sinister Beard
  • 3,570
  • 12
  • 59
  • 95

2 Answers2

0

Using PHP's DOM classes, you could do:

$doc = new \DOMDocument();
@$doc->loadHTML(get_the_content());

$xpath = new \DOMXPath($doc);

foreach ($xpath->query('//*[name() != "img"]') as $node) {
    // do stuff here
}
Francis Eytan Dortort
  • 1,407
  • 14
  • 20
-1

As I wanted to separate the images from the main body of the content, the simplest thing was to put them into an array and then delete them from the DOM before returing it (as suggested here), like so:

$html = str_get_html(wpautop(get_the_content()));

$images = array();

foreach($html->find('img') as $element) :
    $images[] = $element->src; // Puts the image source into an array
    $element->outertext = ''; // and then removes it from the DOM.
endforeach;

echo $html;

print_r($images);

This just returns the content of the post without images and then displays the image array afterwards, which I can then run a foreach on to display.

Community
  • 1
  • 1
Sinister Beard
  • 3,570
  • 12
  • 59
  • 95
  • In your answer it returns all images, not all non-images. You're answering a different question. – pguardiario Oct 29 '15 at 22:02
  • @pguardiario - From my question: "I'm trying to use Simple HTML DOM Parser to look at the content of my WordPress posts and move all images to the end of the post, regardless of where they are in the actual html." This is *exactly* what my answer does. – Sinister Beard Oct 30 '15 at 08:13
  • How am I not doing that? How is the HTML returned not that? And how am I not using Simple HTML Dom to achieve it? – Sinister Beard Oct 30 '15 at 12:24
  • *sigh* - For the last time, `$html->find('img')` returns all img elements, not all non-img elements. – pguardiario Oct 30 '15 at 12:53
  • ...yes. Which I then remove from $html. And return $html. Which is what I wanted to do. Given that it's my question, I think I know what I wanted the answer to be, but, thanks for the input and the downvotes. – Sinister Beard Oct 30 '15 at 14:24
  • That's great that it works for you, but it doesn't answer the question so it doesn't belong here. – pguardiario Oct 30 '15 at 21:55