【问题标题】:PHP stristr false positive for CDATACDATA 的 PHP stristr 误报
【发布时间】:2013-07-22 08:43:00
【问题描述】:

以下 HTML/CSS 来自 Hotmail 发送的 HTML 电子邮件...

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

我只是想从内部样式元素中获取 CSS。有些可能包含 HTML cmets,例如上面的一个或 CDATA。出于某种奇怪的原因,PHP 正在为上面的字符串返回下面的 CDATA 的误报...

 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];
  }

【问题讨论】:

    标签: php false-positive


    【解决方案1】:

    为什么不直接使用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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多