Skip to content
Advertisement

How do I find and remove body selectors in CSS

I am working on my PHP to find the body selectors under the style tags. I want to search for each body selector in the style tags to remove them to prevent them changing the body in my html especially background-color, font-family etc.

Here is the html:

JavaScript

And here is another:

JavaScript

Here is what I want to achieve:

JavaScript

Here is the existing code, but this removes the style tags completely:

JavaScript

I only want to find the body { and body{ in each style tags to remove it.

Can you please show me an example how I can search for the body selector in each style tag to remove it?

Thank you.

Advertisement

Answer

You can use preg_replace to find and remove any body { ... } elements from the style tags:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement