16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?