It is the reverse of : PHP preg_replace: remove style=".." from img tags
Im trying to find an expression for preg_replace, that deletes all inline css styles except for images. For example, I have this text:
<img attribut="value" style="myCustom" attribut='value' style='myCustom' /> <input attribut="value" style="myCustom" attribut='value' style='myCustom'> <span attribut="value" style="myCustom" attribut='value' style='myCustom'> style= </span>
And I need to make it look like:
<img attribut="value" style="myCustom" attribut='value' style='myCustom' /> <input attribut="value" "myCustom" attribut='value' 'myCustom'> <span attribut="value" "myCustom" attribut='value' 'myCustom'> style= </span>
or like this:
<img attribut="value" style="myCustom" attribut='value' style='myCustom' /> <input attribut="value" attribut='value'> <span attribut="value" attribut='value'> style= </span>
It might looks like this
preg_replace('/(\<img[^>]+)(style\=\"[^\"]+\")([^>]+)(>)/', '${1}${3}${4}', $article->text)
The regex issue can be answered with a simple negative assertion:
And a simpler approach might be using querypath (rather than fiddly DOMDocument):