WP_Token_Map::to_array(): array

In this article

Exports the token map into an associate array of key/value pairs.

Description

Example:

$smilies->to_array() === array(
    '8O' => '😯',
    ':(' => '🙁',
    ':)' => '🙂',
    ':?' => '😕',
);

Return

array The lookup key/substitution values as an associate array.

Source

 * @return array The lookup key/substitution values as an associate array.
 */
public function to_array(): array {
	$tokens = array();

	$at            = 0;
	$small_mapping = 0;
	$small_length  = strlen( $this->small_words );
	while ( $at < $small_length ) {
		$key            = rtrim( substr( $this->small_words, $at, $this->key_length + 1 ), "\x00" );
		$value          = $this->small_mappings[ $small_mapping++ ];
		$tokens[ $key ] = $value;

		$at += $this->key_length + 1;
	}

	foreach ( $this->large_words as $index => $group ) {
		$prefix       = substr( $this->groups, $index * ( $this->key_length + 1 ), 2 );
		$group_length = strlen( $group );
		$at           = 0;
		while ( $at < $group_length ) {
			$length = unpack( 'C', $group[ $at++ ] )[1];
			$key    = $prefix . substr( $group, $at, $length );

			$at    += $length;
			$length = unpack( 'C', $group[ $at++ ] )[1];
			$value  = substr( $group, $at, $length );

			$tokens[ $key ] = $value;
			$at            += $length;
		}
	}

User Contributed Notes

You must log in before being able to contribute a note or feedback.