WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ok, I've got content with images removed using this code:

$content = strip_tags(get_the_content(), '<p><a><h2><blockquote><code><ul><li><i><em><strong>');
$content = preg_replace("/\[caption.*\[\/caption\]/", '', $content);
$content = str_replace("[/caption]", '', $content);
echo $content;

Now I want to show the images at the end. I'm trying to use this:

$images = strip_tags(get_the_content(), '<img>');
echo $images;

But it's not working. Any ideas?

share|improve this question
I guess I should clarify. When I use this code, it first shows the content without images, which I expected, then it shows all the content including images. – karllhughes Oct 7 '11 at 19:56

maybe use this regex:

$pattern = "/<a(.*?)href=('|\")([^>]*)('|\")(.*?)><img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)class=('|\")([^>]*)('|\")(.*?)\/><\/a>/i";

function example_replace ($content) {
   global $post;

   $pattern = "/<a(.*?)href=('|\")([^>]*)('|\")(.*?)><img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)class=('|\")([^>]*)('|\")(.*?)\/><\/a>/i";
   $replacement = '<a$1href=$2$3$4$5><img$6src=$7$8.$9$10$11class=$12$13 <strong>imagelink</strong>$14$15$16/><\/a>';
   $content = preg_replace($pattern, $replacement, $content);

   return $content;
share|improve this answer
maybe small one is enough: <img([^<>+]*)> see: regexr.com?2usnk – bueltge Oct 7 '11 at 20:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.