wxr_cdata()
云策文档标注
概述
wxr_cdata() 是一个 WordPress 函数,用于将给定字符串包装在 XML CDATA 标签中,确保数据在导出时正确编码。它处理 UTF-8 验证和编码,常用于 WXR 导出功能。
关键要点
- 函数接受一个字符串或 null 参数,返回包装后的 XML CDATA 字符串。
- 内部使用 wp_is_valid_utf8() 检查 UTF-8 有效性,无效时进行编码转换。
- 主要用于 WordPress 导出功能,如 export_wp() 和相关 wxr_* 函数。
代码示例
function wxr_cdata( $str ) {
$str = (string) $str;
if ( ! wp_is_valid_utf8( $str ) ) {
$str = utf8_encode( $str );
}
// $str = ent2ncr(esc_html($str));
$str = '<![CDATA[' . $str . ']]>';
return $str;
}注意事项
- 参数 $str 可以为 null,函数会将其转换为字符串处理。
- 注释掉的代码行 // $str = ent2ncr(esc_html($str)); 表明早期版本可能包含额外转义,当前版本已简化。
- 此函数自 WordPress 2.1.0 版本引入,稳定用于导出系统。
原文内容
Wraps given string in XML CDATA tag.
Parameters
$strstring|nullrequired-
String to wrap in XML CDATA tag. May be null.
Source
function wxr_cdata( $str ) {
$str = (string) $str;
if ( ! wp_is_valid_utf8( $str ) ) {
$str = utf8_encode( $str );
}
// $str = ent2ncr(esc_html($str));
$str = '', ']]]]>';
return $str;
}
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |