Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Removing some pattern WordPress



I need to remove the pattern

<img src="" alt="" width="300" height="200" />

to blank using preg_replace

can anyone please help me.

Answers (1)


Kyle answers:


$str = 'My string with the image <img alt="" class="" src=""/>src="myimage.png"/>';
$new_str = preg_replace('!<img.*?src="".*?/>!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="" 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!