函数文档

before_last_bar()

💡 云策文档标注

概述

before_last_bar() 是一个 WordPress 函数,用于移除管道分隔字符串中的最后一个项目。如果字符串中没有管道字符,则返回原字符串。

关键要点

  • 函数功能:移除管道分隔字符串的最后一个项目,例如处理 'Role name|User role' 这样的字符串。
  • 参数:$text(必需),一个管道分隔的字符串。
  • 返回值:如果字符串中没有管道字符,返回原字符串;否则返回最后一个管道之前的所有内容。
  • 相关函数:与 translate_user_role()、_c()、translate_with_context() 和 _n_c() 等函数关联,用于翻译和上下文处理。
  • 版本历史:自 WordPress 2.8.0 版本引入。

代码示例

function before_last_bar( $text ) {
    $last_bar = strrpos( $text, '|' );
    if ( false === $last_bar ) {
        return $text;
    } else {
        return substr( $text, 0, $last_bar );
    }
}

📄 原文内容

Removes last item on a pipe-delimited string.

Description

Meant for removing the last item in a string, such as ‘Role name|User role’. The original string will be returned if no pipe ‘|’ characters are found in the string.

Parameters

$textstringrequired
A pipe-delimited string.

Return

string Either $text or everything before the last pipe.

Source

function before_last_bar( $text ) {
	$last_bar = strrpos( $text, '|' );
	if ( false === $last_bar ) {
		return $text;
	} else {
		return substr( $text, 0, $last_bar );
	}
}

Changelog

Version Description
2.8.0 Introduced.