Removing inline styles using php [duplicate]
Asked Answered
P

10

15

I am using php to output some rich text. How can I strip out the inline styles completely?

The text will be pasted straight out of MS Word, or OpenOffice, and into a which uses TinyMCE, a Rich-Text editor which allows you to add basic HTML formatting to the text. However I want to remove the inline styles on the

tags (see below), but preserve the

tags themselves.

<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's 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>
Pixilated answered 21/3, 2010 at 22:11 Comment(2)
aper, rewrite your question! i don't understand it at allKattie
What does your input look like? Is it XHTML?Priscian
K
29

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

$text = preg_replace('#(<[a-z ]*)(style=("|\')(.*?)("|\'))([a-z ]*>)#', '\\1\\6', $text);
Kostival answered 22/3, 2010 at 23:29 Comment(5)
@jakenoble - this works for me. But it breaks when anchor tags have inline style. like <a style="display:inline;" href="abc.com/abc.com">abc</a> This effects the href part also. Can you please help.Incursion
does not work when style is escaped. It should be either refactored to make it work or $text should be replaced with stripcslashes($text)Tutt
A more condensed version looks like this: (style=("|\Z)(.*?)("|\Z)), but only matches the style="" part.Sugarcoat
Worked well for me on WordPress content, thanks @jake-nPercheron
This doesn't work when there is an attribute (e.g. class) before the style attibute.Nitroparaffin
I
21

Here is a preg_replace solution I derived from Crozin's answer. This one allows for attributes before and after the style attribute fixing the issue with anchor tags.

$value = preg_replace('/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$1$3', $value);
Inessential answered 6/12, 2013 at 20:4 Comment(2)
Great response, the accepted solution is also ok but deletes too much in some tags like a(it removes attributes like href). This solution is betterBarozzi
This solution is best because it does not only affect 1 letter tags (p, a etc), it also affects the others (div, span etc)Disulfide
S
7

Use HtmlPurifier

Scandium answered 22/3, 2010 at 10:14 Comment(2)
I could use a third party, but I was hoping there was a simpler solution, like using one line of regexPixilated
Sure. Just be aware of the risks - There will be edge cases with a regexp.Scandium
H
3

You can also use PHP Simple HTML DOM Parser, as follows:

$html = str_get_html(SOME_HTML_STRING);

foreach ($html->find('*[style]') as $item) {
   $item->style = null;
}
History answered 31/3, 2019 at 14:41 Comment(0)
A
2

You could use regular expressions:

$text = preg_relace('#<(.+?)style=(:?"|\')?[^"\']+(:?"|\')?(.*?)>#si', '<a\\1 \\2>', $text);
Algeria answered 21/3, 2010 at 22:22 Comment(5)
see this #1732848Sexagesima
Thanks, but that line doesnt work. I get the error: Parse error: syntax error, unexpected '[' in ... (etc filename)Pixilated
I've forgotten to add escape chars before ' ;)Algeria
Hi Crozin, not sure where I should add an escape character? Do you mean a \ ?Pixilated
@Alon, see the second answer on that page: #1732848 . He has some known HTML which is being reliably generated, so a regex is not a bad solution here.Desmarais
T
2

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

Tesler answered 5/6, 2015 at 13:52 Comment(0)
Y
0

Couldn't you just use strip_tags and leave in the tags you want eg <p>, <strong> etc?

Yulan answered 22/3, 2010 at 4:53 Comment(1)
No, because I want to keep the <p> tags, but I don't want any with inline styles, eg <p style="color:#fff;"> It's the inline style I want to remove without removing the <p>Pixilated
S
0

Why don't you just overwrite the tags. So you will have clean tags without inline styling.

Synonymize answered 22/3, 2010 at 23:38 Comment(0)
Y
0

I found this class very useful for doing strip attributes (especially where there's crazy MS Word formatting all through the text):

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

Yulan answered 23/3, 2010 at 0:26 Comment(1)
Correct updated link semlabs.co.uk/blog/php-strip-attributes-class-xml-htmlBreakthrough
C
-1

I am did need to clear style from img tags and did resolved by this code:

$text = preg_replace('#(<img (.*) style=("|\')(.*?)("|\'))([a-z ]*)#', '<img \\2\\6', $text);
echo  $text;
Curst answered 9/1, 2019 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.