Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP对html注释进行反向查找以提取脚本_Php_Html_Regex_Internet Explorer - Fatal编程技术网

使用PHP对html注释进行反向查找以提取脚本

使用PHP对html注释进行反向查找以提取脚本,php,html,regex,internet-explorer,Php,Html,Regex,Internet Explorer,我对HTML中的IE注释有点问题。 我正在尝试从html字符串解析脚本,这不是很难。 但是,在我的正则表达式中,我正在捕获IE注释的脚本,我真的不希望这样 目前我的脚本是: function get_scripts($html){ preg_match_all('#<script(.*?)</script>#is', $html, $matches); $scripts = ""; if(isset($matches[0]) && is_

我对HTML中的IE注释有点问题。 我正在尝试从html字符串解析脚本,这不是很难。 但是,在我的正则表达式中,我正在捕获IE注释的脚本,我真的不希望这样

目前我的脚本是:

function get_scripts($html){

   preg_match_all('#<script(.*?)</script>#is', $html, $matches);

   $scripts = "";

   if(isset($matches[0]) && is_array($matches[0])){   
      foreach ($matches[0] as $key => $value) {
          $scripts .= $value;
      } 
   }

    return $scripts;
   }


  $html = ' 
    <!--[if lt IE 9]>
      <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <script type="text/javascript">
        var domain_blog_json="";
    </script>
 ';

 echo get_scripts($html);
函数获取脚本($html){
预赛#
var domain_blog_json=“”;
';
echo get_脚本($html);
有人知道如何做到这一点吗? 我曾经尝试过负面回顾,但它不起作用,或者我(当然)是Reg Exp中的一个noob


谢谢!

检查正则表达式,请参见,我认为您可以将表达式修改为以下内容:

<!--.*?-->|<script(.*?)<\/script>
      '/<script>(.*)<\/script>/U'   
“/(.*)/U”

未经测试。但我认为这应该可以修复正则表达式。

您要求在此处使用正则表达式,但它不是解析HTML的正确工具。您可以利用它来执行此任务,而不是尝试使用正则表达式来重新发明轮子

$doc = DOMDocument::loadHTML('
<!--[if lt IE 9]>
  <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript">
   var domain_blog_json="";
</script>
<!--[if lt IE 9]>
  <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
');

foreach($doc->getElementsByTagName('script') as $n) {
   echo $doc->saveHTML($n);
}
$doc=DOMDocument::loadHTML('
var domain_blog_json=“”;
');
foreach($doc->getElementsByTagName('script')作为$n){
echo$doc->saveHTML($n);
}
输出

<script type="text/javascript">
   var domain_blog_json="";
</script>

var domain_blog_json=“”;

这应该是可行的,因为它只捕获非IE
标记之间的内容。除非您想同时捕获两个组或做一些特别的事情,否则实际上不需要查看后面的内容

(<script.+[\n<][^!][^><\/]+\n.+>)

(不要使用正则表达式进行html解析。请使用适当的html解析器,如DomDocument。这将忽略默认值的条件注释。是的,我知道,但我喜欢用艰难的方式来完成。事实上,出于兼容性原因,我假设我不能使用DomDocument。是的,这很奇怪。但您的解决方案是可以的:).最后一个问题:它有什么想法吗?没有,只有没有注释的脚本标记:)我已经做了一个变通方法,当我做循环时,我对注释进行preg_替换。它可以工作,所以没问题:)。