Ask your WordPress questions! Pay money and get answers fast! (more info)

Removing some pattern

  • SOLVED

Hi

I need to remove the pattern

<img src="http://www.mydomain.com/sample.jpg" alt="" width="300" height="200" />

to blank using preg_replace

can anyone please help me.

Answers (1)

2014-11-28

Kyle answers:

Try:

$str = 'My string with the image <img alt="" class="" src="http://www.mydomain.com/sample.jpg"/>src="myimage.png"/>';
$new_str = preg_replace('!<img.*?src="http://www.mydomain.com/sample.jpg".*?/>!i', '', $str);


You'll need to replace the src in the second line and the first line is the string that contains the image


bappa2543 comments:

the fact that the sample.jpg img is random


bappa2543 comments:


<img src="http://www.mydomain.com/samplerandom_image.jpg" alt="" width="300" height="200" />


Kyle comments:

This will remove all images if that is what you mean:


$string = 'This is the original string with the image';
$string = preg_replace('/<(\s*)img[^<>]*>/i', '', $string);


bappa2543 comments:

wow thank you its work like charm


Kyle comments:

You're very welcome!