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:

<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.

Advertisement

Answer

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('/bbodys*{[^}]+}/', '', $style->nodeValue);
    }
}
$inbox_message = $doc->saveHTML();
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement