函数文档

wp_basename()

💡 云策文档标注

概述

wp_basename() 是 WordPress 中 basename() 函数的国际化友好版本,用于从路径中提取文件名,并支持可选后缀移除。

关键要点

  • 函数原型:wp_basename( $path, $suffix = '' ),接受路径字符串和可选后缀参数。
  • 核心功能:通过 URL 编码/解码处理路径中的特殊字符(如 %2F 和 %5C),确保在多语言环境下正确提取文件名。
  • 返回值:返回处理后的文件名字符串,如果指定后缀且文件名以该后缀结尾,则移除后缀。

代码示例

wp_basename('module-1-your-introduction-to-floristry.zip', '.zip');
// 返回:module-1-your-introduction-to-floristry

📄 原文内容

i18n-friendly version of basename().

Parameters

$pathstringrequired
A path.
$suffixstringrequired
If the filename ends in suffix this will also be cut off.

Return

string

Source

function wp_basename( $path, $suffix = '' ) {
	return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
}

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes