DOMDocument::loadHTML(): Empty string supplied as input
php
Solution
Alright @Bruce..I understand the issue now. What you want to do is test the value of `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";
}
}
?>
So `if ($str === FALSE) { //return an error }` and don't let the script continue. You could return false like I am doing or just do an if/else.
Problem
I have search through countless pages trying to find an answer that actually works. I have tried libraries files to specifically deal with warning and error handling, yet even when I suppress all warnings and errors, this one final warning is still showing: ``` Warning: DOMDocument::loadHTML(): Empty string supplied as input ``` My php processing is below. The code works perfectly as long as the user enters an actual url, however when the user inputs data that is not a url the warning above is displayed. ``` 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"; } } ``` How can I suppress this warning? It seems the suggestion seems to be stop suppressing warnings and instead fix them, so I am listing all the warnings when I stop suppressing them ``` 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 ``` Keep in mind I am scanning user input url's so I have no control over the format of the page being tested - meaning I can't fix their code. So what do I do if not suppress warnings?