note 29984 added to function.uksort

From: Date: Mon, 03 Mar 2003 10:47:10 +0000
Subject: note 29984 added to function.uksort
Groups: php.notes 
Request: Send a blank email to php-notes+get-44770@lists.php.net to get a copy of this message
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&#269;&#268;..."; // 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; } } -- http://www.php.net/manual/en/function.uksort.php http://master.php.net/manage/user-notes.php?action=edit+29984 http://master.php.net/manage/user-notes.php?action=delete+29984 http://master.php.net/manage/user-notes.php?action=reject+29984

« previous php.notes (#44770) next »