• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

removing 'width' attribute from image tag using preg_replace

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 ?
  • 2
1 Solution
Try this

Open in new window

amitbravoAuthor 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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now