Removing inline styles using php

I am using php to output some rich text. How to completely erase inline styles?

The text will be inserted directly from MS Word or OpenOffice and TinyMCE, the Rich-Text editor that will allow you to add basic HTML formatting to the text, will be used in it. However I want to remove inline styles in tags

(see below), but keep the tags

.

<p style="margin-bottom: 0cm;">A patrol of Zograth apes came round the corner, causing Rosette to pull Rufus into a small alcove, where she pressed her body against his. &ldquo;Sorry.&rdquo; She said, breathing warm air onto the shy man neck. Rufus trembled.</p> <p style="margin-bottom: 0cm;">&nbsp;</p> <p style="margin-bottom: 0cm;">Rosette checked the coast was clear and pulled Rufus out of their hidey hole. They watched as the Zograth walked down a corridor, almost out of sight and then collapsed next to a phallic fountain. As their bodies hit the ground, their guns clattered across the floor. Rosette stopped one with her heel and picked it up immediately, tossing the other one to Rufus. &ldquo;Most of these apes seem to be dying, but you might need this, just to give them a helping hand.&rdquo;</p> 
+10
php styles inline
source share
9 answers

I quickly put this together, but for "inline styles" (!) You will need something like

 $text = preg_replace('#(<[az ]*)(style=("|\')(.*?)("|\'))([az ]*>)#', '\\1\\6', $text); 
+27
source share

Here is the preg_replace solution obtained from Krozin's answer. This allows attributes before and after the style attribute fixing a problem with anchor tags.

 $value = preg_replace('/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$1$3', $value); 
+10
source share
+5
source share

You can use regular expressions:

 $text = preg_relace('#<(.+?)style=(:?"|\')?[^"\']+(:?"|\')?(.*?)>#si', '<a\\1 \\2>', $text); 
+2
source share

Could you just use strip_tags and leave it in the tags you want, for example, <p>, <strong> , etc.

0
source share

Why don't you just rewrite the tags. This way you will have clean tags without inline style.

0
source share

I found this class very useful for creating strip attributes (especially where the crazy formatting of MS Word throughout the text):

http://semlabs.co.uk/journal/php-strip-attributes-class-for-xml-and-html

0
source share

You can use: $ content = preg_replace ('/ style = [^>] * /', '', $ content);

0
source share

I need to clear the style of IMG tags and solved with this code:

 $text = preg_replace('#(<img (.*) style=("|\')(.*?)("|\'))([az ]*)#', '<img \\2\\6', $text); echo $text; 
0
source share

All Articles