LoginSignup
16
14

More than 5 years have passed since last update.

Warning: DOMDocument::loadHTML() を抑制する

Posted at

HTML5 な文書を DOMDocument::loadHTML 使って読み込ませると Warning が出る。

<?php
$html = '<!doctype html><html><body><video loop><source src="test.mp4"></source></video></body></html>';
$dom = new \DOMDocument();
$dom->loadHTML( $html );
var_dump( $dom->getElementsByTagName( "video" )->length );

//Warning: DOMDocument::loadHTML(): Tag video invalid in Entity, line: 1 in /path/to/sample.php on line 4
//Warning: DOMDocument::loadHTML(): Tag source invalid in Entity, line: 1 in /path/to/sample.php on line 4
// int(1)

loadHTML の前後に libxml_use_internal_errorslibxml_clear_errors を呼び出せばエラー抑制できる。

<?php
$html = '<!doctype html><html><body><video loop><source src="test.mp4"></source></video></body></html>';
$dom = new \DOMDocument();
libxml_use_internal_errors( true );  // これと
$dom->loadHTML( $html );
libxml_clear_errors();  // これ
var_dump( $dom->getElementsByTagName( "video" )->length );

// int(1)
16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14