wp_split_selector_list()WP 7.1.0

Splits a selector list by top-level commas.

No Hooks.

Returns

string[]. Selectors.

Usage

wp_split_selector_list( $selector );
$selector(string) (required)
CSS selector list.

Changelog

Since 7.1.0 Introduced.

wp_split_selector_list() code WP 7.1

function wp_split_selector_list( $selector ) {
	if ( ! str_contains( $selector, ',' ) ) {
		return array( $selector );
	}

	$selectors         = array();
	$current_selector  = '';
	$parentheses_depth = 0;
	$selector_length   = strlen( $selector );

	for ( $i = 0; $i < $selector_length; $i++ ) {
		$char = $selector[ $i ];

		if ( '(' === $char ) {
			++$parentheses_depth;
		} elseif ( ')' === $char && $parentheses_depth > 0 ) {
			--$parentheses_depth;
		} elseif ( ',' === $char && 0 === $parentheses_depth ) {
			$selectors[]      = $current_selector;
			$current_selector = '';
			continue;
		}

		$current_selector .= $char;
	}

	$selectors[] = $current_selector;

	return $selectors;
}