Possible Duplicate:
PHP - remove <img> tag from string
I need to remove an image tag from a string and at the same time replace it with something. Here is what I have:
$body = '<p>Lorem ipsum dolor sit amet:</p>
<p><img class="news" id="" src="images/news_48.png" alt="" /></p>
<p>Curabitur tincidunt vehicula mauris, nec facilisis nisl ultrices sit amet:</p>
<p><img class="gallery" id="26" src="images/gallery_48.png" alt="" /></p>
<p><img id="this_image_must_stay" src="images/some_image.png" alt="" /></p>';
I need to do one thing if the image has class="news" and another thing if it's class="gallery". I'm thinking some pseudocode like:
<?php
if(news){
replace the image tag where class=news with %%news%%
}
if(gallery){
replace the image tag where class=gallery with %%gallery%%
assign the value 26 to some variable
}
?>
So now $body
would contain:
$body = '<p>Lorem ipsum dolor sit amet:</p>
<p>%%news%%</p>
<p>Curabitur tincidunt vehicula mauris, nec facilisis nisl ultrices sit amet:</p>
<p>%%gallery%%</p>
<p><img id="this_image_must_stay" src="images/some_image.png" alt="" /></p>';
I guess I have to use preg_match/replace, but I'm not good at regex. Any help is appreciated.