DOMDocument::loadHTML(): warning - htmlParseEntityRef: no name in Entity
This correct answer comes from a comment from @lonesomeday.
My best guess then is that there is an unescaped ampersand (&) somewhere in the HTML. This will make the parser think we're in an entity reference (e.g. ). When it gets to ;, it thinks the entity is over. It then realises what it has doesn't conform to an entity, so it sends out a warning and returns the content as plain text.
PHP Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity
You probably have an HTML file being referenced by "test" that is being used by new App()
, however that HTML file contains errors. Most likely (almost certain) cause is something like <a href="sompage.php?foo=bar&cat=fish">Link</a>
. The &
in the HREF introduces a character entity, but there is no ;
to go with it.
How to loadHTMLFile() when it fails with 'htmlParseEntityRef: no name' error?
If you really must try to parse it, try this:
<?php
$html = file_get_contents("http://gisapps.co.union.nc.us/ws/rest/v2/cm_iw.ashx?gid=12339");
$doc = new DOMDocument();
$doc->strictErrorChecking = false;
$doc->recover=true;
@$doc->loadHTML("<html><body>".$html."</body></html>");
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*/div[@class='owner-name']");
if (!is_null($elements)) {
foreach ($elements as $element) {
echo "<br/>[". $element->nodeName. "]";
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
?>
PS: Your XPath was wrong, I fixed it. Your $nodes
won't have anything because that DIV element (.owner-name
) doesn't have any children.. so you'll need to revise that.
DOMDocument::loadHTML(): Empty string supplied as input
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.
Related Topics
Read Each Line of Txt File to New Array Element
How to Check If a File Is Mp3 or Image File
Switch PHP Versions on Commandline Ubuntu 16.04
Alternative to MySQL_Real_Escape_String Without Connecting to Db
How to Use Sha1 Encryption Instead of Bcrypt in Laravel 4
Assigning the Return Value of New by Reference Is Deprecated
How Exactly Do Regular Expression Word Boundaries Work in PHP
How to Remove HTML Special Chars
PHP Getting Domain Name from Subdomain
Find Out How PHP Is Running on Server (Cgi or Fastcgi or Mod_PHP)
Json_Decode Returns Json_Error_Syntax But Online Formatter Says the Json Is Ok
PHP Redirection With Post Parameters
PHP Regex: How to Match \R and \N Without Using [\R\N]
Converting MySQL Result Array to Json
Best Way to Completely Destroy a Session - Even If the Browser Is Not Closed