【发布时间】:2020-07-08 18:27:51
【问题描述】:
我正在使用我的 PHP 来查找样式标签下的 body 选择器。我想在样式标签中搜索每个body 选择器以删除它们以防止它们更改我的html 中的正文,尤其是background-color、font-family 等。
这里是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>
还有一个:
<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>
这是我想要实现的目标:
<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>
这是现有代码,但这完全删除了样式标签:
$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);
}
}
我只想在每个样式标签中找到body {和body{将其删除。
您能告诉我一个示例,我如何在每个样式标签中搜索body 选择器以将其删除?
谢谢。
【问题讨论】:
标签: php html css preg-replace domdocument