略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 简介

2024-12-23

简介

The DOM extension allows you to operate on XML documents through the DOM API with PHP.

注意:

此 DOM 扩展采用 UTF-8 编码。在 ISO-8859-1 编码下,使用 utf8_encode()utf8_decode() 来处理,其它编码下使用 iconv 函数处理。

add a noteadd a note

User Contributed Notes 2 notes

up
1
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
-20
Anonymous
2 years ago
On some linux package managers, namely apt, this is called 'xml' for example as in:

sudo apt-get install -y php7.3-xml

官方地址:https://www.php.net/manual/en/intro.dom.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3