<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代码的内容
答案 0 :(得分:1)
这是你正在寻找的吗?
<style[\S\s]*?<\/style>
(Example)
答案 1 :(得分:0)
这样的事情会对你有所帮助
$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;
答案 2 :(得分:0)
你不必须使用带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>
节点(及其内容)。
答案 3 :(得分:0)
作为实际删除样式标记和内容的替代方法,再次使用DOMDocument,您可以简单地捕获和处理文档正文的内容。
$dom=new DOMDocument;
$dom->loadHTML( $subject[ $i ] );
$body=$dom->getElementsByTagName('body')->item(0)->nodeValue;
echo $body;