2016-10-27 12 views
1

実際に動作する回答を見つけようと無数のページから検索しました。私は、特に警告やエラー処理に対処するためのライブラリファイルを試してみましたが、まだ私はすべての警告やエラーを抑制した場合でも、この1つの最後の警告は引き続き表示されます。DOMDocument :: loadHTML():空の文字列が入力として提供されました

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のようにスキャンしています覚えておいてくださいを抑制する停止したとき、私はすべての警告を一覧表示しています私はテストされているページの形式を制御することはできません。つまり、コードを修正できません。

警告を表示しない場合はどうすればよいですか?

+1

警告を抑止するのではなく、状況を確認するコードを作成します。 'if($ str)' {'あなたのもの}'または 'if($ str!= ''){...}' – Ronnie

+1

'article_url'は引用符で囲まなければなりません。警告を表示しない場合は、あなたのページだけで何もしませんか? – chris85

+0

'_ + 1'はchris85のクォートに関するコメントで、' file_get_contents'の前に@を削除して問題があるかどうかを確認します。 –

答えて

3

Alright @ 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 }の値をテストし、スクリプトを続行させてはいけないです。あなたは私がやっているように偽を返すことができますか、または単にif/elseを行うことができます。

関連する問題