Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing inline styles using php [duplicate]

Tags:

php

styles

inline

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>
like image 612
Onion Avatar asked Mar 21 '10 22:03

Onion


People also ask

How do I get rid of inline style?

To remove inline Styles from an existing page, open your page with the editor, select the paragraph (or the entire content by pressing CTRL + A) and then click on Remove Formatting button. Inline styles, while they have a purpose, are not the best way to maintain your Web site.

How do you avoid inline styles?

Instead of using inline styles, use external stylesheets. They give you all the benefits of CSS best practices and are easy to use. Employed in this way, all the styles used on your site live in a separate document that is then linked to a web document with a single line of code.

Can inline style be reused?

You cannot reuse the styles anywhere else.

Does important override inline styles?

important and inline style. Adding the ! important keyword to any CSS rule lets the rule forcefully precede over all the other CSS rules for that element. It even overrides the inline styles from the markup.


1 Answers

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);
like image 198
Jake N Avatar answered Sep 28 '22 14:09

Jake N