Remove inline CSS and classes from text with PHP

I have a text in this form:

aaaa bbbbb cccccc a:link {text-decoration: none;font-family: Verdana, Arial, Helvetica, sans-serif;color: #ffffff; } a:hover {text-decoration: underline; } .intro{font-size: 11px;font-weight: bold;line-height: 18px;color : #ffffff;padding-left: 25px;font-family: Verdana, Arial, Helvetica, sans-serif; } ddddd eeeeee

I would like to remove all the css with the classes. The output should be:

aaaa bbbbb cccccc ddddd eeeeee

Can aynone show me an preg_match example? I fond an example to remove everything between the brakets {} but I need, that everything with css is removed.

Thanks Nik


The trouble you will have with removing the non-CSS parts from that string is that it's very hard to determine which parts are CSS and which aren't.

You say you want to be left with aaaa bbbbb cccccc ddddd eeeeee , but in fact from your original string, aaaa bbbbb cccccc would be valid parts of the CSS selector. They would select elements named <aaaa> or <bbbbb> or <cccccc> .

Granted, these are not valid HTML elements, but CSS can be used to apply styles to arbitrary XML as well as HTML, so they could very easily be valid elements. If you're using xhtml, they could appear in your page quite legitimately under a custom name-space.

But it gets worse. I assume that the text wouldn't actually be aaaa bbbbb cccccc , but would be an arbitrary string of words. In that case, consider that it may be a sentence like 'I am strong'. In this case, strong would be part of the string you want to remove, but <strong> is also a valid HTML element (as is <I> for that matter), so even if you are just sticking with it would be impossible to tell in the above string whether it was intended to be a CSS selector or part of the text string to keep. You simply couldn't write a regex that would be completely reliable in all cases.

As you say, you can remove everything inside the {} braces fairly easily, but the selectors outside the braces would be very hard to separate reliably from surrounding text.

链接地址: http://www.djcxy.com/p/87562.html

上一篇: asp.net菜单控制ie8和ie9问题

下一篇: 使用PHP从文本中删除内联CSS和类