3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML <!DOCTYPE html> <head> <meta charset="EUC-JP"> </head> <body> <h1>テスト文書</h1> <p>これはテスト用のHTMLです。EUC-JPと宣言しているのにShift_JISでエンコードされています。</p> </body> HTML; $html = mb_convert_encoding($html, 'SJIS-win'); // そのまま読み込むと当然文字化けする $doc = \Dom\HTMLDocument::createFromString($html, \Dom\HTML_NO_DEFAULT_NS); $xpath = new \Dom\XPath($doc); $node = $xpath->query("//h1")->item(0); var_dump($node->textContent); // 一度UTF-8にしてから、第3引数でエンコードを固定 $html = mb_convert_encoding($html, 'UTF-8', 'SJIS-win'); $doc = \Dom\HTMLDocument::createFromString($html, \Dom\HTML_NO_DEFAULT_NS, 'UTF-8'); $xpath = new \Dom\XPath($doc); $node = $xpath->query("//h1")->item(0); var_dump($node->textContent);
Output for 8.4.1 - 8.4.14, 8.5.0
string(21) "�e�X�g���" string(15) "テスト文書"
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 8.1.32, 8.2.25 - 8.2.29, 8.3.5 - 8.3.28
Fatal error: Uncaught Error: Class "Dom\HTMLDocument" not found in /in/iio1m:15 Stack trace: #0 {main} thrown in /in/iio1m on line 15
Process exited with code 255.

preferences:
50.46 ms | 409 KiB | 5 Q