1

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

3
  • why not use preg_replace for your condition? Commented Jan 31, 2011 at 7:07
  • Accept answers and show us the regex you already have. You've only given us half the ingredients for your answer Commented Jan 31, 2011 at 9:53
  • Remove the brackets after removing the CSS inside the brackets? Commented Apr 12, 2018 at 3:00

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Find cccccc.*?ddddd and Replace it with a blank space

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.