WordPress 函数:wp_parse_list 分割字符串成数组

江河/ 2023年10月28日/ WordPress/ 浏览 454

函数原型:


wp_parse_list( array|string $input_list ): array


将标量值的逗号或空格分隔列表转换为数组。


参数说明:


$input_list, 字符串,英文逗号分割,或者空格分割。


函数源码:


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;
}


使用举例:


// 结果 $result 是 [1,2,3]
$result = wp_parse_list('1,2,3');


发表评论

暂无评论,抢个沙发...

客服 工单