2

Possible Duplicate:
PHP - remove <img> tag from string

I have my content like this:

$content = '<p>&nbsp;</p>
<p><img width="645" height="450" src="/proshore/userfiles/image/1.jpg" alt="" /></p>
<p>An explorer adventures into an unknown world, yet it seems that he has been there      before. A short animated film directed by Malcolm Sutherland in 2010. With music by Alison Melville and Ben Grossman, and foley by Leon Lo. Sound design / mix by Malcolm Sutherland.</p>
<p>The animation is all hand-drawn; a mix of drawing / pastels on paper and digital animation with Toonboom Studio and a wacom cintiq tablet, assembled in After Effects 7 and edited in Sony Vegas 8.</p>
<p>&nbsp;</p>';

I want the output ignoring the <img> tag. I tried some messups with preg_replace but didn't work.. It will be a great help if someone can explain me how it works.

Community
  • 1
  • 1
Anita Sharma
  • 175
  • 4
  • 13
  • 2
    What have you tried? HTML/XML-parsing/manipulation via REGEX is normally a bad idea. If the input is even slightly malformed or unpredictable, the pattern will fail. You may want to look into [PHP:DOM](http://php.net/manual/en/book.dom.php) – Mitya Jul 16 '12 at 11:08
  • Can you write what output you want? – Alexey Jul 16 '12 at 11:08
  • No need to use regular expressions here. There's an a̶p̶p̶ function for that! ;) It's called [`strip_tags()`](http://php.net/manual/en/function.strip-tags.php) – Lix Jul 16 '12 at 11:17

3 Answers3

1

If you are not forced to use regular expressions to parse HTML content, then I'd suggest using PHP's strip_tags() function along with its allowed_tags argument.

This will tell PHP to remove all the html tags and leave the ones your specified in the allowed_tags.

Example usage taken from the documentation -

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text); 
// output : Test paragraph. Other text

// Allow <p> and <a>
echo strip_tags($text, '<p><a>'); 
// output - <p>Test paragraph.</p> <a href="#fragment">Other text</a>

So, if you simply specify all the HTML tags in allow_tags except the <img> tag, you should get the results that you need - only removing the <img> tag from your string.

Community
  • 1
  • 1
Lix
  • 47,311
  • 12
  • 103
  • 131
  • 1
    Although this solution will work without problems, but it will leave an unwanted `

    ` which is now around the ``, also you don't always know what tags can occur and listing them all omitting just the image - it's just not the best practice..
    – Zoltan Toth Jul 16 '12 at 11:17
  • The OP did not describe the reason for this functionality - for all we know empty holder tags could be a desired effect. This answer is based only on the information that was provided. I agree with you (somewhat) with regard to explicitly specifying *all* HTML tags.. maybe not the best way... but I'm open to suggestions :P – Lix Jul 16 '12 at 11:21
  • thanks @zol - for the constructive criticism! – Lix Jul 16 '12 at 11:21
  • 1
    thanks :) the intention was really constructive, because the solution is perfectly functional and achieves what the OP asked for – Zoltan Toth Jul 16 '12 at 11:47
1

try:

 $content = preg_replace("/<img[^>]+\>/i", " ", $content);
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
0

Replace this regex <img.+?((/>)|(</img>)) with empty string

Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
burning_LEGION
  • 13,246
  • 8
  • 40
  • 52