【问题标题】:PHP remove conditional comments which cause DOMXPath failPHP 删除导致 DOMXPath 失败的条件注释
【发布时间】:2013-09-22 11:36:23
【问题描述】:

有条件的 cmets,例如:

<!--[if lt IE 7 ]><!--> <html lang="hi" class="ie ie6"> <!--<![endif]-->

原因

$doc->loadHTML($content);   
$xpath = new DOMXPath($doc);

无法正常工作。有没有准备好的代码来删除它们?

【问题讨论】:

  • 为我工作.. 3v4l.org/tNfPX
  • 如果其中几个确实存在于一个页面中怎么办?我只想要 IE html

标签: php domxpath conditional-comments


【解决方案1】:

如果解析 HTML 不起作用,那么您可以使用正则表达式,例如以下示例:

$str = "aa<!--[if lt IE 7 ]><!--> <html lang=\"hi\" class=\"ie ie6\"> <!--<![endif]-->bb cc";
$pattern = "/(<!--[if\s[lg]+t\sIE\s[0-9]+\s]><!-->)(.*)(<!--<!\[endif\]-->)/"; 
$str = preg_replace($pattern, '', $str);    
echo $str;

或者保留条件标签的内容:

$str = preg_replace($pattern, '$2', $str);
echo $str;

【讨论】:

  • 如果在&lt;!--[if lt IE 7 ]&gt;&lt;!--&gt;&lt;!--&lt;![endif]--&gt; 之间写了别的东西怎么办?
猜你喜欢
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 2020-04-01
相关资源
最近更新 更多