0

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.

1 Answer 1

1

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();
4
  • Thank you, when I try this it didn't find all body to remove it. Any idea? Mar 28, 2020 at 0:31
  • @chrisoojer for the two examples you gave it will remove them. Can you edit your question with an example where it didn't work?
    – Nick
    Mar 28, 2020 at 0:33
  • Sorry I didn't realised that it is working now. I used echo $inbox_message; as it didn't work so when I try to use $inbox_message = $doc->saveHTML(); and then echo $inbox_message;. Now it is working!! :D Mar 28, 2020 at 0:38
  • @chrisoojer yeah sorry, I should have put that in the code. I'll edit the answer.
    – Nick
    Mar 28, 2020 at 0:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.