函数文档

untrailingslashit()

💡 云策文档标注

概述

untrailingslashit() 是 WordPress 核心函数,用于移除字符串末尾的斜杠(包括正斜杠和反斜杠)。主要应用于路径处理,但也可用于其他字符串场景。

关键要点

  • 函数功能:移除字符串末尾的斜杠('/' 和 '')。
  • 主要用途:处理路径字符串,确保路径格式规范。
  • 参数:接受一个必需字符串参数 $value,返回移除末尾斜杠后的字符串。
  • 实现方式:基于 PHP 的 rtrim() 函数,指定移除字符为 '/'。
  • 相关函数:与 trailingslashit() 互补,后者用于添加末尾斜杠。

代码示例

function untrailingslashit( $value ) {
    return rtrim( $value, '/' );
}

📄 原文内容

Removes trailing forward slashes and backslashes if they exist.

Description

The primary use of this is for paths and thus should be used for paths. It is not restricted to paths and offers no specific path support.

Parameters

$valuestringrequired
Value from which trailing slashes will be removed.

Return

string String without the trailing slashes.

Source

function untrailingslashit( $value ) {
	return rtrim( $value, '/\' );
}

Changelog

Version Description
2.2.0 Introduced.