函数文档

_cleanup_header_comment()

💡 云策文档标注

概述

_cleanup_header_comment() 是一个 WordPress 核心函数,用于清理文件头注释中的关闭标签。它通过正则表达式移除字符串中的 */ 或 ?> 及其后的内容,常用于处理主题或插件的元数据提取。

关键要点

  • 函数作用:从 WordPress 文件头注释中去除关闭注释标签(*/)和关闭 PHP 标签(?>)及其后续内容。
  • 参数:接受一个字符串参数 $str,表示需要清理的头部注释。
  • 返回值:返回清理后的字符串,使用 trim() 去除首尾空白。
  • 实现方式:使用 preg_replace() 正则表达式 '/s*(?:*/|?>).*/' 进行匹配和替换。
  • 相关函数:与 WP_Theme::get_post_templates()、get_file_description() 和 get_file_data() 等函数关联,用于文件元数据处理。
  • 版本历史:自 WordPress 2.8.0 版本引入。

代码示例

function _cleanup_header_comment( $str ) {
    return trim( preg_replace( '/s*(?:*/|?>).*/', '', $str ) );
}

📄 原文内容

Strips close comment and close php tags from file headers used by WP.

Description

See also

Parameters

$strstringrequired
Header comment to clean up.

Return

string

Source

function _cleanup_header_comment( $str ) {
	return trim( preg_replace( '/s*(?:*/|?>).*/', '', $str ) );
}

Changelog

Version Description
2.8.0 Introduced.