-1
<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>

This the html content I get when i read the mail through imap function I want to remove the style tag and the content inside the style tag and then print/echo it in php. I have i have used preg_match but it does not work

preg_replace("/<style\\b[^>]*>(.*?)<\\/style>/s", "", $subject[$i])

The $subject[$i] is the content in which I have the above html code

RAJ
  • 229
  • 2
  • 14
  • if you remove the style from the above there will be nothing left - where is the body? Rather than `preg_replace` use a `DOMDocument` and remove the `style` that way possibly – Professor Abronsius Feb 23 '16 at 11:38
  • There is conent in the body but I don't want to put it so I removed the body code from the question – RAJ Feb 23 '16 at 11:40

4 Answers4

1

Is this what you're looking for?

<style[\S\s]*?<\/style> (Example)

Prime
  • 2,410
  • 1
  • 20
  • 35
  • This does not work with me when I print_r($subject[$i]) . get the text in between the style tags . I want to remove the text also . In simple word complete remove the – RAJ Feb 23 '16 at 11:50
  • Are you actually using `$subject[$i] = preg_replace('/ – Prime Feb 23 '16 at 11:52
  • Yes I am doing the same thing as you mention in your comment above – RAJ Feb 23 '16 at 11:54
  • Is [this](https://3v4l.org/ICHmJ) not the intended output? **Edit:** Are you wanting to echo the HTML or the contents of the `style` tags? – Prime Feb 23 '16 at 11:58
  • No , Don't want to echo the contents of the style tag – RAJ Feb 23 '16 at 12:10
  • Yes want the same thing – RAJ Feb 23 '16 at 12:15
  • But I'm using the line of code in the comment above with the HTML you provided, so the problem would have be something else in your code wouldn't it? – Prime Feb 23 '16 at 12:18
0

something like this will help you

$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;
Drudge Rajen
  • 7,584
  • 4
  • 23
  • 43
0

You don't have to use regular expression with HTML, you have to use a parser:

$dom = new DOMDocument();
libxml_use_internal_errors( True );
$dom->loadHTML( $subject[$i] );

$styles = $dom->getElementsbyTagName( 'style' );
$styles->item(0)->parentNode->removeChild( $styles->item(0) );

By this way you will remove first <style> node (with its contents).


Community
  • 1
  • 1
fusion3k
  • 11,568
  • 4
  • 25
  • 47
0

As an alternative to actually removing the style tags and content, again - using DOMDocument, you can simply capture and work with the contents of the document body.

$dom=new DOMDocument;
$dom->loadHTML( $subject[ $i ] );
$body=$dom->getElementsByTagName('body')->item(0)->nodeValue;
echo $body;
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46