note 101394 modified in function.natcasesort by danbrown
| From: | danbrown@php.net | Date: | Tue, 14 Dec 2010 17:49:30 +0000 |
| Subject: | note 101394 modified in function.natcasesort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174915@lists.php.net to get a copy of this message | ||
Sorting UTF-8 by arbitrary order:
<?php
mb_internal_encoding("UTF-8");
class utf_8_german
{
// everything else is sorted at the end
static $order = '0123456789AaÃäBbCcDdEeFfGgHhIiJjKkLlMm
NnOoÃöPpQqRrSsÃTtUuÃüVvWwXxYyZz';
static $char2order;
static function cmp($a, $b) {
if ($a == $b) {
return 0;
}
// lazy init mapping
if (empty(self::$char2order))
{
$order = 1;
$len = mb_strlen(self::$order);
for ($order=0; $order<$len; ++$order)
{
self::$char2order[mb_substr(self::$order, $order, 1)] = $order;
}
}
$len_a = mb_strlen($a);
$len_b = mb_strlen($b);
$max=min($len_a, $len_b);
for($i=0; $i<$max; ++$i)
{
$char_a= mb_substr($a, $i, 1);
$char_b= mb_substr($b, $i, 1);
if ($char_a == $char_b) continue;
$order_a = (isset(self::$char2order[$char_a])) ? self::$char2order[$char_a] : 9999;
$order_b = (isset(self::$char2order[$char_b])) ? self::$char2order[$char_b] : 9999;
return ($order_a < $order_b) ? -1 : 1;
}
return ($len_a < $len_b) ? -1 : 1;
}
}
// usage example:
$t = array(
'Birnen', 'Birne', 'Ãpfel', 'Apfel',
);
uasort($t, 'utf_8_german::cmp');
echo '$t:
<pre>'.htmlspecialchars(print_r($t,true),null,'UTF-8').'</pre>';
?>
--was--
Sorting UTF-8 by arbitrary order:
mb_internal_encoding("UTF-8");
class utf_8_german
{
// everything else is sorted at the end
static $order = '0123456789AaÃäBbCcDdEeFfGgHhIiJjKkLlMm
NnOoÃöPpQqRrSsÃTtUuÃüVvWwXxYyZz';
static $char2order;
static function cmp($a, $b) {
if ($a == $b) {
return 0;
}
// lazy init mapping
if (empty(self::$char2order))
{
$order = 1;
$len = mb_strlen(self::$order);
for ($order=0; $order<$len; ++$order)
{
self::$char2order[mb_substr(self::$order, $order, 1)] = $order;
}
}
$len_a = mb_strlen($a);
$len_b = mb_strlen($b);
$max=min($len_a, $len_b);
for($i=0; $i<$max; ++$i)
{
$char_a= mb_substr($a, $i, 1);
$char_b= mb_substr($b, $i, 1);
if ($char_a == $char_b) continue;
$order_a = (isset(self::$char2order[$char_a])) ? self::$char2order[$char_a] : 9999;
$order_b = (isset(self::$char2order[$char_b])) ? self::$char2order[$char_b] : 9999;
return ($order_a < $order_b) ? -1 : 1;
}
return ($len_a < $len_b) ? -1 : 1;
}
}
// usage example:
$t = array(
'Birnen', 'Birne', 'Ãpfel', 'Apfel',
);
uasort($t, 'utf_8_german::cmp');
echo '$t:
<pre>'.htmlspecialchars(print_r($t,true),null,'UTF-8').'</pre>';
http://php.net/manual/en/function.natcasesort.php