$code ) { // Prefix search if ( strpos( $name, $searchKey, 0 ) === 0 ) { $results[$code] = $name; } } return $results; } public static function getIndex( $name ) { $codepoint = self::getCodepoint( $name ); if ( $codepoint < 1000 ) { $bucket = $codepoint; } else { $bucket = $codepoint % 1000; } if ( !isset( $buckets[$bucket] ) ) { $buckets[$bucket] = array(); } return $bucket; } /** * Get the code point of first letter of string * * @return integer Code point of first letter of string */ static function getCodepoint( $str ) { $unicode = array(); $values = array(); $lookingFor = 1; for ( $i = 0; $i < strlen( $str ); $i++ ) { $thisValue = ord( $str[$i] ); if ( $thisValue < 128 ) { return $thisValue; } else { // Codepoints larger than 127 are represented by multi-byte sequences, if ( count( $values ) === 0 ) { // 224 is the lowest non-overlong-encoded codepoint. $lookingFor = ( $thisValue < 224 ) ? 2 : 3; } $values[] = $thisValue; if ( count( $values ) === $lookingFor ) { // Refer http://en.wikipedia.org/wiki/UTF-8#Description $number = ( $lookingFor === 3 ) ? ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ) : ( ( $values[0] % 32 ) * 64 ) + ( $values[ 1] % 64 ); return $number; } } } } }