我搜索了无数页,试图找到一个真正有效的答案。我尝试过库文件专门处理警告和错误处理,但是即使当我抑制所有警告和错误时,这一最后的警告仍然显示:
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";
}
}
我怎样才能压制这个警告?
这个建议似乎是停止压制警告,而是修正它们,所以当我停止压制警告时,我会列出所有的警告。
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
记住,我正在扫描用户输入url,所以我无法控制被测试页面的格式--这意味着我无法修复他们的代码。
那么,如果不压制警告,我该怎么办呢?
发布于 2016-10-29 18:32:40
好了,布鲁斯.我现在明白了。您要做的是测试file_get_contents()
的值。
<?php
error_reporting(-1);
ini_set("display_errors", 1);
$article_url = 'http://google.com';
if (isset($article_url)){
$title = 'contact us';
$str = @file_get_contents($article_url);
// return an error
if ($str === FALSE) {
echo 'problem getting url';
return false;
}
// Continue
$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 ($str === FALSE) { //return an error }
和不要让脚本继续。您可以像我正在做的那样返回false,也可以只执行if/else。
发布于 2020-02-25 09:41:05
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'));
https://stackoverflow.com/questions/40295681
复制相似问题