Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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 DOMDocument::loadHTML():作为输入提供的空字符串_Php - Fatal编程技术网

Php DOMDocument::loadHTML():作为输入提供的空字符串

Php DOMDocument::loadHTML():作为输入提供的空字符串,php,Php,我搜索了无数页,试图找到一个真正有效的答案。我已经尝试使用库文件专门处理警告和错误处理,但即使我抑制了所有警告和错误,最后一个警告仍然显示: Warning: DOMDocument::loadHTML(): Empty string supplied as input 下面是我的php处理。只要用户输入一个实际的url,代码就可以正常工作,但是当用户输入的数据不是url时,就会显示上面的警告 if (isset($_GET[article_url])){ $title = 'cont

我搜索了无数页,试图找到一个真正有效的答案。我已经尝试使用库文件专门处理警告和错误处理,但即使我抑制了所有警告和错误,最后一个警告仍然显示:

Warning: DOMDocument::loadHTML(): Empty string supplied as input
下面是我的php处理。只要用户输入一个实际的url,代码就可以正常工作,但是当用户输入的数据不是url时,就会显示上面的警告

if (isset($_GET[article_url])){
    $title = 'contact us';
    $str = @file_get_contents($_GET[article_url]);
    $test1 = str_word_count(strip_tags(strtolower($str)));
    if($test1 === FALSE) { $test = '0'; }
    if ($test1 > '550') {
        echo '<div><i class="fa fa-check-square-o" style="color:green"></i> This article has '.$test1.' words.';
    } else {
        echo '<div><i class="fa fa-times-circle-o" style="color:red"></i> This article has '.$test1.' words. You are required to have a minimum of 500 words.</div>';
    }

    $document = new DOMDocument();
    $libxml_previous_state = libxml_use_internal_errors(true);
    $document->loadHTML($str);
    libxml_use_internal_errors($libxml_previous_state);

    $tags = array ('h1', 'h2');
    $texts = array ();

    foreach($tags as $tag)
    {
        $elementList = $document->getElementsByTagName($tag);
        foreach($elementList as $element)
        {
            $texts[$element->tagName] = strtolower($element->textContent);
        }
    }

    if(in_array(strtolower($title),$texts)) {
        echo '<div><i class="fa fa-check-square-o" style="color:green"></i> This article used the correct title tag.</div>';
    } else {
    echo "no";
    }
}
if(isset($\u GET[article\u url])){
$title=‘联系我们’;
$str=@file\u get\u contents($\u get[article\u url]);
$test1=str_单词计数(strip_标签(strtolower$str));
如果($test1==FALSE){$test='0';}
如果($test1>‘550’){
echo“这篇文章有“$test1.”单词;
}否则{
echo“这篇文章有“$test1.”个单词。你必须至少有500个单词。”;
}
$document=新的DOMDocument();
$libxml\u previous\u state=libxml\u use\u internal\u errors(true);
$document->loadHTML($str);
libxml_使用_内部_错误($libxml_先前_状态);
$tags=数组('h1','h2');
$text=array();
foreach($tags作为$tag)
{
$elementList=$document->getElementsByTagName($tag);
foreach($elementList作为$element)
{
$text[$element->tagName]=strtolower($element->textContent);
}
}
if(在数组中(strtolower($title),$text)){
echo“本文使用了正确的标题标签”;
}否则{
回应“否”;
}
}
如何抑制此警告

建议似乎是停止抑制警告,而是修复它们,因此我在停止抑制警告时列出了所有警告

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity
Warning: DOMDocument::loadHTML(): htmlParseStartTag: misplaced <body> tag in Entity
Warning: DOMDocument::loadHTML(): Tag header invalid in Entity
Warning: DOMDocument::loadHTML(): Tag section invalid in Entity
Warning: DOMDocument::loadHTML(): error parsing attribute name in Entity
Warning: DOMDocument::loadHTML(): Tag footer invalid in Entity
Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity
DOMDocument::loadHTML(): Unexpected end tag : strong in Entity
警告:DOMDocument::loadHTML():HTMLParserEntityRef:应为“;”在实体中
警告:DOMDocument::loadHTML():htmlParseStartTag:实体中的标记放错位置
警告:DOMDocument::loadHTML():标记头在实体中无效
警告:DOMDocument::loadHTML():标记节在实体中无效
警告:DOMDocument::loadHTML():分析实体中的属性名称时出错
警告:DOMDocument::loadHTML():标记页脚在实体中无效
警告:DOMDocument::loadHTML():HTMLParserEntityRef:实体中没有名称
DOMDocument::loadHTML():实体中意外的结束标记:strong
请记住,我正在扫描用户输入的url,因此我无法控制正在测试的页面的格式,这意味着我无法修复它们的代码


那么,如果不抑制警告,我该怎么办

好了@Bruce..我现在明白了。您要做的是测试
file\u get\u contents()的值。



因此,
if($str==FALSE){//返回错误}
并且不要让脚本继续。你可以像我正在做的那样返回false,或者只是做if/else。

好的@Bruce..我现在理解这个问题了。您要做的是测试
file\u get\u contents()的值。

return $this->subject($data['subject'])->markdown('mails.send_instant_notification', compact('data'));

因此,
if($str==FALSE){//返回错误}
并且不要让脚本继续。你可以像我一样返回false,或者只是执行if/else

return $this->subject($data['subject'])->markdown('mails.send_instant_notification', compact('data'));
如果将标记用作模板生成,请不要包含内联样式。但是,如果您使用的是视图,则可以包含内联css样式

return $this->subject($data['subject'])
                ->view('mails.send_instant_notification', compact('data'));
如果将标记用作模板生成,请不要包含内联样式。但是,如果您使用的是视图,则可以包含内联css样式

return $this->subject($data['subject'])
                ->view('mails.send_instant_notification', compact('data'));

与其抑制警告,不如编写代码检查设置的内容
if(!empty($str)){do your stuff}
if($str!=''){…}
文章url
应加引号。如果你抑制了警告,你的页面不是什么也做不了吗?
\uu1
关于chris85引号注释,删除@before
文件\u get\u contents
看看是否有问题。@Ronnie我也尝试过这种方法,它抑制了所有警告,除了我正在显示的警告。…@chris85没有抑制警告,将取消显示警告。如果有人在url输入中提交了非url。。。。那就意味着它什么也没做。。。但是如果用户输入一个非url,它还能做什么呢。。。很明显,它不能做什么。与其抑制警告,不如编写代码检查设置
if(!empty($str)){do your stuff}
if($str!=''){…}
文章url
应加引号。如果你抑制了警告,你的页面不是什么也做不了吗?
\uu1
关于chris85引号注释,删除@before
文件\u get\u contents
看看是否有问题。@Ronnie我也尝试过这种方法,它抑制了所有警告,除了我正在显示的警告。…@chris85没有抑制警告,将取消显示警告。如果有人在url输入中提交了非url。。。。那就意味着它什么也没做。。。但是如果用户输入一个非url,它还能做什么呢。。。很明显,它做不了什么。