PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How do I find and remove body selectors in CSS

 November 20, 2022     css, domdocument, html, php, preg-replace     No comments   

Issue

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:

<style type="text/css">
 /*GENERAL*/
    table{width:100%}
    body{background-color:#ebebeb; width: 100%; margin:0; padding:0; -webkit-font-smoothing: antialiased;font-family: "Segoe UI",SegoeUI,"Helvetica Neue",Helvetica,sans serif; -webkit-text-size-adjust: 100%;}
    div.ms-article-container #emailbodyouter .emailbodyinner section{margin:0}
    div.ms-article-container #emailbodyouter .emailbodyinner table div {margin:0}
    div.content-article #emailbodyouter .emailbodyinner section{margin:0}
    div.content-article #emailbodyouter .emailbodyinner table div {margin:0}
</style>

And here is another:

<style type="text/css"> body {position: relative; font-family: Segoe UI; font-size: 12px; } .pageHeader {color: #9C9C9C; font-size: 160%; padding: 0px 0px 6px 0px} .pageHeaderLogo {padding-right: 15px;} .pageHeaderTitle{border-left: 1px solid #CCCCCC; padding: 5px;} .pageFooter {width: 100%; background-color: #f2f2f2; font-size: 12px; font-family: Segoe UI; padding:4px 4px 4px 4px; } .pageFooterLogo {text-align:right; width:100%} .padCells { padding: 0px 6px 0px 0px; } .preHeader {display: none !important; visibility:hidden; opacity:0; color:transparent; height:0; width:0; }</style>

Here is what I want to achieve:

<style type="text/css">
     /*GENERAL*/
        table{width:100%}
        div.ms-article-container #emailbodyouter .emailbodyinner section{margin:0}
        div.ms-article-container #emailbodyouter .emailbodyinner table div {margin:0}
        div.content-article #emailbodyouter .emailbodyinner section{margin:0}
        div.content-article #emailbodyouter .emailbodyinner table div {margin:0}
    </style>

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

$doc = new DOMDocument();
$doc->loadHTML($inbox_message);
$xpath = new DOMXpath($doc);
$styles = $xpath->query('//style[@type="text/css"]');
if ($styles) {
  foreach ($styles as $style) {
    $style->parentNode->removeChild($style);
  }
}

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.


Solution

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

$doc = new DOMDocument();
$doc->loadHTML($inbox_message);
$xpath = new DOMXpath($doc);
$styles = $xpath->query('//style[@type="text/css"]');
if ($styles) {
    foreach ($styles as $style) {
        $style->nodeValue = preg_replace('/\bbody\s*\{[^}]+}/', '', $style->nodeValue);
    }
}
$inbox_message = $doc->saveHTML();


Answered By - Nick
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing