函数文档

wp_parse_list()

💡 云策文档标注

概述

wp_parse_list() 函数用于将逗号或空格分隔的标量值列表转换为数组。它接受数组或字符串输入,并返回一个经过验证的标量值数组。

关键要点

  • 参数 $input_list 可以是数组或字符串,为必需参数,表示要转换的值列表。
  • 返回值是一个数组,包含转换后的标量值。
  • 如果输入是字符串,函数使用正则表达式按逗号或空格分割;如果输入是数组,则过滤掉非标量值。
  • 该函数自 WordPress 5.1.0 版本引入。

代码示例

$string = 'apple, banana, cherry';
$items = wp_parse_list( $string );
print_r( $items );

📄 原文内容

Converts a comma- or space-separated list of scalar values to an array.

Parameters

$input_listarray|stringrequired
List of values.

Return

array Array of values.

Source

function wp_parse_list( $input_list ) {
	if ( ! is_array( $input_list ) ) {
		return preg_split( '/[s,]+/', $input_list, -1, PREG_SPLIT_NO_EMPTY );
	}

	// Validate all entries of the list are scalar.
	$input_list = array_filter( $input_list, 'is_scalar' );

	return $input_list;
}

Changelog

Version Description
5.1.0 Introduced.

User Contributed Notes