Created
October 22, 2011 15:35
-
-
Save rodneyrehm/1306118 to your computer and use it in GitHub Desktop.
PHP: mb_range() - Unicode compatible range('A', 'Z')
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
mb_internal_encoding('UTF-8'); | |
/** | |
* multibyte string compatible range('A', 'Z') | |
* | |
* @param string $start Character to start from (included) | |
* @param string $end Character to end with (included) | |
* @return array list of characters in unicode alphabet from $start to $end | |
* @author Rodney Rehm | |
*/ | |
function mb_range($start, $end) { | |
// if start and end are the same, well, there's nothing to do | |
if ($start == $end) { | |
return array($start); | |
} | |
$_result = array(); | |
// get unicodes of start and end | |
list(, $_start, $_end) = unpack("N*", mb_convert_encoding($start . $end, "UTF-32BE", "UTF-8")); | |
// determine movement direction | |
$_offset = $_start < $_end ? 1 : -1; | |
$_current = $_start; | |
while ($_current != $_end) { | |
$_result[] = mb_convert_encoding(pack("N*", $_current), "UTF-8", "UTF-32BE"); | |
$_current += $_offset; | |
} | |
$_result[] = $end; | |
return $_result; | |
} | |
var_dump(mb_range('क', 'म')); | |
var_dump(mb_range('A', 'C')); | |
var_dump(mb_range('A', 'Z')); | |
// beware, ä…ö are not a sequence you'd expect! | |
var_dump(mb_range('ä', 'ö')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tegardazzly
You can encode decimals to Nth base number more than N=64.
For example, the code below you can shorten MD5 hash to 10 Chinese characters.