如何使用PHP从HTML中删除<style> </style>及其内部内容

时间:2014-11-26 08:30:03

标签: php html regex

我有以下示例HTML,我想从中删除。我用过这个正则表达式:

/<(style)(?:(?!<\\/?\\1).)*?<\\/\\1>/s

的输出是:

1
Array
(
    [0] => <style type="text/css">
pre{display:none !important;}
</style>
    [1] => style
)

表示其中的任何内容,这些标记应该分开,其他HTML应保留,因为它是分开的。所以最后我们将样式标签数据放在一个变量中,将其他HTML放在其他变量中。

以下是样本:

<style type="text/css">
pre{display:none !important;}
</style>
<div class="main-container">
  <div class="page-title">asd</div>
  <div class="stepone"> asd<a href="asd">asd</a></div>
  <div class="stepthree"> asd</div>
  <div class="bookgift">assadsad</div>
  <div class="registration"> asd</div>
  <div class="given"> asd</div>
  <div class="too"> asd</div>
</div><style type="text/css">
.main-container {margin-left: auto; margin-right: auto; width: auto; font-family: Calibri; text-align: center;}
.page-title {font-family: Calibri; font-size:18px; text-align: center;}
.stepone {padding: 10px;}
.steptwo {padding: 10px;}
.codeis {font-size: 25px; text-align: center;}
.code {font-size: 50px; padding: 8px;}
.stepthree {padding: 10px;}
.bookgift {font-weight: bold;text-align: center;}
.registration {padding: 10px;}
.given {font-size: 20px;font-weight: bold;padding: 10px;}
.too {font-size: 20px;font-weight: bold;padding: 10px;}
img { text-align: center; clear: both; margin:10px; }
.figcenter {margin: auto; text-align: center; vertical-align: middle; margin-top: 1.5em; margin-bottom: 1.5em; clear: both !important;}
.steptwoss { font-family: myraid pro; font-size: 15px; }
</style>

期望的输出:

在一个变量中只有HTML:

<div class="main-container">
  <div class="page-title">asd</div>
  <div class="stepone"> asd<a href="asd">asd</a></div>
  <div class="stepthree"> asd</div>
  <div class="bookgift">assadsad</div>
  <div class="registration"> asd</div>
  <div class="given"> asd</div>
  <div class="too"> asd</div>

另一个应该只有Style数据和标签:

<style type="text/css">
pre{display:none !important;}
</style>
<style type="text/css">
.main-container {margin-left: auto; margin-right: auto; width: auto; font-family: Calibri; text-align: center;}
.page-title {font-family: Calibri; font-size:18px; text-align: center;}
.stepone {padding: 10px;}
.steptwo {padding: 10px;}
.codeis {font-size: 25px; text-align: center;}
.code {font-size: 50px; padding: 8px;}
.stepthree {padding: 10px;}
.bookgift {font-weight: bold;text-align: center;}
.registration {padding: 10px;}
.given {font-size: 20px;font-weight: bold;padding: 10px;}
.too {font-size: 20px;font-weight: bold;padding: 10px;}
img { text-align: center; clear: both; margin:10px; }
.figcenter {margin: auto; text-align: center; vertical-align: middle; margin-top: 1.5em; margin-bottom: 1.5em; clear: both !important;}
.steptwoss { font-family: myraid pro; font-size: 15px; }
</style>

0 个答案:

没有答案