Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.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中抛出致命错误,如何在下一个循环中继续执行?_Php_Exception Handling_Error Handling_Web Scraping_Fatal Error - Fatal编程技术网

如果函数在PHP中抛出致命错误,如何在下一个循环中继续执行?

如果函数在PHP中抛出致命错误,如何在下一个循环中继续执行?,php,exception-handling,error-handling,web-scraping,fatal-error,Php,Exception Handling,Error Handling,Web Scraping,Fatal Error,我给你要点 我正在尝试使用一个新的URL来抓取某些URL,因为我认为默认的strip_tags()做得不好。(我认为你不需要检查刮刀) 现在有时候,一些网站的HTML源代码包含一些奇怪的代码,导致我的HTML标记剥离器失败 其中一个示例包含以下代码段: <li><a href="<//?=$cnf['website']?>girls/models-photo-gallery/?sType=6#top_menu">Photo Galleries</a>

我给你要点

我正在尝试使用一个新的URL来抓取某些URL,因为我认为默认的strip_tags()做得不好。(我认为你不需要检查刮刀)

现在有时候,一些网站的HTML源代码包含一些奇怪的代码,导致我的HTML标记剥离器失败

其中一个示例包含以下代码段:

<li><a href="<//?=$cnf['website']?>girls/models-photo-gallery/?sType=6#top_menu">Photo Galleries</a></li>
这里myfunc进行处理,并使用我前面提到的第三方HTML剥离器。 我尝试将代码修改为:

foreach ($results as $result)
    {
        $url=$result->Url;
        $worddicttemp=array();
        try
        {
            $worddicttemp=myfunc($url,$worddict2,$history,$n_gram); //returns the string represenation of what matters, hopefully
            //The below line will be executed only when the above function doesn't throw a fatal error
            $worddict2=$worddicttemp;
        }
        catch(Exception $e)
        {
            continue;
        }
    }
但我还是犯了同样的错误。
怎么了?为什么myfunc()中的代码在遇到致命错误时立即将控制权转移到catch块?

我建议您在解析之前使用一些更漂亮的脚本。您的问题可以通过添加

$html_content = htmlspecialchars($html_content)

您无法捕获解析错误(或任何致命错误,但解析错误更糟糕,因为它们将在加载代码后立即生成)。我所知道的隔离它们的最好方法是对任何您想要恢复的内容运行完全独立的PHP进程,并期望生成致命错误


另请参见

使用strstrstr检查$worddicttemp中是否有错误如果为true,则使用continue to next URL使用preg_replace'e'修饰符的HTML剥离器非常疯狂。我会寻找一些其他的解决方案,因为所讨论的功能正朝着dodo的方向发展。我相信preg_*的eval修饰符迟早会被删除,最好现在就删除。是的,它将被弃用。在这种情况下使用它无论如何都是疯狂的。
$html_content = htmlspecialchars($html_content)