note 29984 deleted from function.uksort by cmb
| From: | cmb@php.net | Date: | Sat, 27 Aug 2016 18:33:16 +0000 |
| Subject: | note 29984 deleted from function.uksort by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206405@lists.php.net to get a copy of this message | ||
Note Submitter: pachollini at stones dot com
----
I tried to write my own function for sorting with special Czech characters, which php normally
compares wrong way. The function is recoursive, it compares the first characters of the strings and
when they're the same, the function calls itself with parameters without the first character.
My php often crashed by calling this function. After some time if found the problem: it crashed
after 10th recoursive call. I think it's a bug in php, and I've made this workaround:
function str_compare($a,$b,$level=0)
{
$maxlevel=9;
if($GLOBALS["STR_COMPARE"] && is_array($a))
{
$a=$a[$GLOBALS["STR_COMPARE"]];
$b=$b[$GLOBALS["STR_COMPARE"]];
}
$result=0;
if($a==$b) return 0;
elseif($a=="") return ($b=="") ? 0 : 1;
elseif ($b=="") return -1;
else
{
$order="aAáÁbBcCčČ..."; // longer normally & without that html
entities
for($i=0;$i<strlen($order);$i++) $codes[]=ord($order[$i]);
$char_a=$a[0];$char_b=$b[0];
$pos_a=array_search(ord($a),$codes); $pos_b=array_search(ord($b),$codes);
//echo"$char_a - $pos_a<br>";
if($pos_a===false || $pos_b===false)
{
if($char_a==$char_b)
{
if ($level<=$maxlevel) $result=str_compare (substr($a,1),substr($b,1),$level+1);
else $a<$b ? $result=-1 : $result=1;
}
else ($char_a<$char_b) ? $result=-1 : $result=1;
}
else
{
if($pos_a==$pos_b)
{
if ($level<=$maxlevel) $result=str_compare(substr($a,1), substr($b,1),$level+1);
else $a<$b ? $result=-1 : $result=1;
}
else ($pos_a<$pos_b) ? $result=-1 : $result=1;
}
return $result;
}
}