PHP 8.1.15 Released!

はじめに

DOM 拡張モジュールを使用すると、DOM API を使用した XML ドキュメントの操作を PHP で行えます。

注意:

DOM拡張モジュール は UTF-8 エンコーディングを使います。他のエンコーディングを扱う場合は、mb_convert_encoding(), UConverter::transcode(), iconv() を使ってください。

add a note

User Contributed Notes 2 notes

up
6
captainjester at hotmail dot com
2 years ago
Be careful when using this for partial HTML. This will only take complete HTML documents with at least an HTML element and a BODY element. If you are working on partial HTML and you fill in the missing elements around it and don't specify in META elements the character encoding then it will be treated as ISO-8859-1 and will mangle UTF-8 strings. Example:

<?php
$body
= getHtmlBody();
$doc = new DOMDocument();
$doc->loadHtml("<html><body>".$body."</body></html>");
// $doc will treat your HTML ISO-8859-1.
// this is correct but may not be what you want if your source is UTF-8
?>

<?php
$body
= getHtmlBody();
$doc = new DOMDocument();
$doc->loadHtml("<html><head><meta charset=\"UTF-8\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body>".$body."</body></html>");
// $doc will treat your HTML correctly as UTF-8.
?>
up
-1
greezlykz at gmail dot com
1 month ago
you can use below code to load html in utf-8 format. It is not enough that your encoding is in utf-8.

$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="UTF-8">' . $html);
To Top