room11/dom-utils

PHP DOM 扩展的实用函数

v1.1.1 2016-10-19 15:15 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:47:31 UTC


README

PHP DOM 扩展的实用函数。

所需 PHP 版本

  • PHP 7.0+

安装

$ composer require room11/dom-utils

用法

default_charset

获取和设置 DOMUtils 函数使用的默认字符集。

default_charset(string $charSet = ''): string

当提供的 $charSet 为空时,仅返回当前值。当提供新值时,更改默认值并返回旧值。

  • string $charSet - 新的默认字符集。

domdocument_load_html

将 HTML 字符串加载到 DOMDocument 对象中,具有错误处理和字符集归一化。

domdocument_load_html(string $html, int $options = 0, string $charSet = ''): \DOMDocument

将 HTML 字符串加载到 DOMDocument 对象中,具有错误处理和字符集归一化。

  • string $html - 要加载的 HTML 字符串。
  • int $options - 加载文档时使用的 LibXML 选项的位掩码。
  • string $charSet - 加载文档时使用的字符集。默认为 default_charset()

在加载文档失败时抛出 LibXMLFatalErrorException

domdocument_process_html_docs

将一组 HTML 字符串处理为 DOMDocument 对象,并对每个文档调用回调函数。

domdocument_process_html_docs($htmlStrings, callable $callback, int $options = 0, string $charSet = ''): \DOMDocument

回调函数的第一个参数传递一个 DOMDocument 对象,第二个参数传递文档的 LibXMLError 对象数组。第三个参数是一个布尔值,如果文档无法加载则为 true。回调函数可以返回布尔值 false 来中断迭代。任何其他返回值将被忽略。

  • string[] $htmlStrings - 要处理的 HTML 字符串的可迭代集合。
  • callable(\DOMDocument, \LibXMLError[], bool) $callback - 对每个文档调用的回调函数。
  • int $options - 加载文档时使用的 LibXML 选项的位掩码。
  • string $charSet - 加载文档时使用的字符集。默认为 default_charset()