removing 'width' attribute from image tag using preg_replace

amitbravo
amitbravo used Ask the Experts™
on
my site users are using WYSWYG editor .. they insert image like
<img src="http://www.techie-diva.com/images/myimg.jpg" width="500" height="200" />
I want to remove width="<some value>" and height attribute from the tag before insert data into database
is preg_replace or any other php function will help me to cut if off ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this
preg_replace('/(width|height)="[^"]*"/i','',$imgtag)

Open in new window

Author

Commented:
but this way this can remove attribute of other tags such as table , etc . i only need  to remove tag of <img src.. tag only
Then you can use something like this.
<?
$text='<img src="http://www.techie-diva.com/images/myimg.jpg" width="500" y="d" height="200" a="e" b="4" />
<table heght="200" width="678"></table>
<img src="http://www.techie-diva.com/images/myimg.jpg" width="500" height="200" />';
echo preg_replace("!(<img)([^<>]+)(/>)!e","'\\1'.preg_replace('!(height|width)=\"[^\"]*\"!i','',\"\\2\").'\\3'",$text),"\n";
?>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial