Frage

The following HTML/CSS is from an HTML email sent from Hotmail...

<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style>

I'm merely trying to get the CSS from inside style elements. Some may contain HTML comments such as the one above or CDATA. For some strange reason PHP is returning a false-positive for CDATA below for the string above...

 if (stristr($b,'<style'))
 {
  $s = explode('<style',$b,2)[1];
  $s = explode('>',$s,2)[1];

  if (stristr($s,'<![CDATA['))
  {
   $s = explode('<![CDATA[',$s,2)[1];
   $s = explode(']]',$s,2)[0];
  }
  else if (stristr($s,'<!--'))
  {
   $s = explode('<!--',$s,2)[1];
   $s = explode('-->',$s,2)[0];
  }
  else
  {
   $s = explode('</style>',$s,2)[0];
  }
War es hilfreich?

Lösung

Why not just take the DOMDocument?

$html = "
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style>";


$dom = new DOMDocument();
$dom->loadHTML($html);
$style = $dom->getElementsByTagName('style');

// get the content from first style tag
$css = $style->item(0)->nodeValue;
// clear the comments and cdata tags
$css = str_replace(array('<!--', '-->', '<![CDATA[', ']]>', '//<![CDATA[', '//]]>'), '', $css);
echo $css;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top