<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<meta name='Generator' content='Microsoft Word 14 (filtered medium)'>
<style><!--
/* Font Definitions */
@font-face
{ font-family:'Cambria Math';
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{ font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{ font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:'Times New Roman','serif';}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p
{mso-style-priority:99;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:12.0pt;
font-family:'Times New Roman','serif';}
span.EmailStyle19
{mso-style-type:personal-reply;
font-family:'Calibri','sans-serif';
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
{page:WordSection1;}
-->
</style>
</head>
</html>
这是我通过 imap 函数阅读邮件时获得的 html 内容,我想删除样式标签和样式标签内的内容,然后在 php 中打印/回显它。我用过preg_match但它不起作用
preg_replace("/<style''b[^>]*>(.*?)<''/style>/s", "", $subject[$i])
$subject[$i]是我有上述html代码的内容
这是你要找的吗?
<style['S's]*?<'/style>
(示例)
您不必在HTML中使用正则表达式,您必须使用解析器:
$dom = new DOMDocument();
libxml_use_internal_errors( True );
$dom->loadHTML( $subject[$i] );
$styles = $dom->getElementsbyTagName( 'style' );
$styles->item(0)->parentNode->removeChild( $styles->item(0) );
通过这种方式,您将删除第一个<style>
节点(及其内容)。
- 阅读更多 关于 DOMDocument
- 阅读为什么你不能用正则表达式解析 [X]HTML
这样的东西会对你有所帮助
$txt=
' <html>
<head>
<title></title>
<style>
body{
background:#000;
}
</style>
</head>
<body>
hello world
</body>
</html>';
$a= preg_replace('/(<(style)'b[^>]*>).*?(<'/'2>)/is', "", $txt);
print_r($a);exit;
作为实际删除样式标签和内容的替代方法,再次使用 DOMDocument,您可以简单地捕获和使用文档正文的内容。
$dom=new DOMDocument;
$dom->loadHTML( $subject[ $i ] );
$body=$dom->getElementsByTagName('body')->item(0)->nodeValue;
echo $body;