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;
}
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 usegetElementsByTagName()
to get all the images.http://www.php.net/manual/en/domdocument.getelementsbytagname.php
Trivial task for
DOMDocument
:You need to watch out though,
saveHTML()
might add missing tags around your structure.