【问题标题】:DOMDocument::loadHTML(): Empty string supplied as inputDOMDocument::loadHTML():作为输入提供的空字符串
【发布时间】:2016-10-27 23:20:48
【问题描述】:

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

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,因此我无法控制被测试页面的格式——这意味着我无法修复他们的代码。

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

【问题讨论】:

  • 与其抑制警告,不如编写检查设置的代码。 if (!empty($str)) { do your stuff }if ($str != '') { ... }
  • article_url 应该用引号引起来。如果您抑制警告,您的页面不会什么都不做吗?
  • _+1关于chris85引用评论,去掉file_get_contents前面的@看看有没有问题。
  • @Ronnie 我也尝试过这种方法,它会抑制除我正在显示的警告之外的所有警告......
  • @chris85 没有抑制警告,将抑制警告。如果有人在 url 输入中提交了一个非 url ......这意味着它什么都不做......但是如果用户输入一个非 url 它还能做什么......显然它不能做点什么。

标签: php


【解决方案1】:

好的@Bruce..我现在明白这个问题了。你要做的是测试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。

【讨论】:

    【解决方案2】:
    return $this->subject($data['subject'])->markdown('mails.send_instant_notification', compact('data'));
    

    如果您使用 markdown 作为模板构建,请不要包含内联样式。但如果您使用的是视图,则可以包含内联 CSS 样式。

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

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 2012-02-27
      • 1970-01-01
      • 2012-05-18
      • 2019-10-10
      相关资源
      最近更新 更多