saveDomDocument()
云策文档标注
概述
saveDomDocument() 函数用于将 DOMDocument 对象保存为 XML 文件,通过处理换行符确保兼容性。
关键要点
- 函数接受两个必需参数:$doc(DOMDocument 对象)和 $filename(字符串文件名)。
- 内部使用 saveXML() 获取 XML 字符串,并通过正则表达式将换行符标准化为 rn。
- 通过 fopen()、fwrite() 和 fclose() 将处理后的 XML 写入指定文件。
代码示例
function saveDomDocument( $doc, $filename ) {
$config = $doc->saveXML();
$config = preg_replace( "/([^r])n/", "$1rn", $config );
$fp = fopen( $filename, 'w' );
fwrite( $fp, $config );
fclose( $fp );
}注意事项
- 函数名不符合 WordPress 命名规范,但被忽略(phpcs:ignore)。
- 自 WordPress 2.8.0 版本引入,用于 IIS 7+ 配置文件的 rewrite 规则管理。
原文内容
Saves the XML document into a file.
Parameters
$docDOMDocumentrequired$filenamestringrequired
Source
function saveDomDocument( $doc, $filename ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
$config = $doc->saveXML();
$config = preg_replace( "/([^r])n/", "$1rn", $config );
$fp = fopen( $filename, 'w' );
fwrite( $fp, $config );
fclose( $fp );
}
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |