Delete the style attribute of some HTML tags in a document and replace it with a class attribute


For instance I have a string:

$string = '<div class="ImageRight" style="width:150px">';

which I want to transform into this:

$string = '<div class="ImageRight">';

  1. I want to remove the portion style="width:150px with preg_replace() where the size 150 can vary, so the width can be 500px etc. aswell.

  2. Also, the last part of the classname varies aswell, so the class can be ImageRight, ImageLeft, ImageTop etc.

So, how can I remove the style attribute completely from a string with the above mentioned structure, where the only things that varies is the last portion of the classname and the width value?

EDIT: The ACTUAL string I have is an entire html document and I don't want to remove the style attribute from the entire html, only from the tags which match the string I've shown above.

I think this is what you're after...

$modifiedHtml = preg_replace('/<(div class="Image[^"]+") style="[^"]+">/i', '<$1>', $html);