2

I've recently just imported all of the content from a blogger site into wordpress and I need to tidy things up a bit.

I'm working inside the single.php and I want to get each <a><img src=""/></a> from the_content();. My php is a little shoddy at best.

I understand this gets me the first image of the post, but I need something similar, one that gets me all the images (not featured images) from the_content();.

function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches[1][0];

    if(empty($first_img)) {
        $first_img = "/path/to/default.png";
    }
    return $first_img;
} 
UzumakiDev
  • 1,286
  • 2
  • 17
  • 39

2 Answers2

4

Take a look at the PHP's native DOMDocument object. http://www.php.net/manual/en/class.domdocument.php

You would take your content and load it into a DOMDocument via loadHTML(). THen you can use getElementsByTagName() to get all the images.

http://www.php.net/manual/en/domdocument.getelementsbytagname.php

$document = new DOMDocument();
$document->loadHTML($post->post_content);
$images = $document->getElementsByTagName('img');
Schleis
  • 41,516
  • 7
  • 68
  • 87
2

Trivial task for DOMDocument:

$doc = new DOMDocument();
$doc->loadHTML($post->post_content);
foreach ($doc->getElementsByTagName('img') as $img)
    $img->attributes['src'] = '/path/to/default.png'; // or whatever you want to do
return $doc->saveHTML();

You need to watch out though, saveHTML() might add missing tags around your structure.

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102