返回介绍

_split_str_by_whitespace()

发布于 2017-09-11 13:37:13 字数 3128 浏览 1113 评论 0 收藏 0

Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

_split_str_by_whitespace( string $string,  int $goal )

Breaks a string into chunks by splitting at whitespace characters.


description

The length of each returned chunk is as close to the specified length goal as possible, with the caveat that each chunk includes its trailing delimiter. Chunks longer than the goal are guaranteed to not have any inner whitespace.

Joining the returned chunks with empty delimiters reconstructs the input string losslessly.

Input string must have no null characters (or eventual transformations on output chunks must not care about null characters)

_split_str_by_whitespace( "1234 67890 1234 67890a cd 1234   890 123456789 1234567890a    45678   1 3 5 7 90 ", 10 ) ==
array (
    0 => '1234 67890 ',  // 11 characters: Perfect split
    1 => '1234 ',        //  5 characters: '1234 67890a' was too long
    2 => '67890a cd ',   // 10 characters: '67890a cd 1234' was too long
    3 => '1234   890 ',  // 11 characters: Perfect split
    4 => '123456789 ',   // 10 characters: '123456789 1234567890a' was too long
    5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split
    6 => '   45678   ',  // 11 characters: Perfect split
    7 => '1 3 5 7 90 ',  // 11 characters: End of $string
);

参数

$string

(string) (Required) The string to split.

$goal

(int) (Required) The desired chunk length.


返回值

(array) Numeric array of chunks.


源代码

File: wp-includes/formatting.php

function _split_str_by_whitespace( $string, $goal ) {
	$chunks = array();

	$string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" );

	while ( $goal < strlen( $string_nullspace ) ) {
		$pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" );

		if ( false === $pos ) {
			$pos = strpos( $string_nullspace, "\000", $goal + 1 );
			if ( false === $pos ) {
				break;
			}
		}

		$chunks[] = substr( $string, 0, $pos + 1 );
		$string = substr( $string, $pos + 1 );
		$string_nullspace = substr( $string_nullspace, $pos + 1 );
	}

	if ( $string ) {
		$chunks[] = $string;
	}

	return $chunks;
}

更新日志

Versiondescription
3.4.0Introduced.

相关函数

Used By

  • wp-includes/formatting.php: make_clickable()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文