note 68380 added to function.strnatcasecmp

From: Date: Tue, 25 Jul 2006 07:49:17 +0000
Subject: note 68380 added to function.strnatcasecmp
Groups: php.notes 
Request: Send a blank email to php-notes+get-115117@lists.php.net to get a copy of this message
There seems to be a bug in the localization for strnatcmp and strnatcasecmp. I searched the reported bugs and found a few entries which were up to four years old (but the problem still exists when using swedish characters). These functions might work instead. <?php function _strnatcasecmp($left, $right) { return _strnatcmp(strtolower($left), strtolower($right)); } function _strnatcmp($left, $right) { while((strlen($left) > 0) && (strlen($right) > 0)) { if(preg_match('/^([^0-9]*)([0-9].*)$/Us', $left, $lMatch)) { $lTest = $lMatch[1]; $left = $lMatch[2]; } else { $lTest = $left; $left = ''; } if(preg_match('/^([^0-9]*)([0-9].*)$/Us', $right, $rMatch)) { $rTest = $rMatch[1]; $right = $rMatch[2]; } else { $rTest = $right; $right = ''; } $test = strcmp($lTest, $rTest); if($test != 0) { return $test; } if(preg_match('/^([0-9]+)([^0-9].*)?$/Us', $left, $lMatch)) { $lTest = intval($lMatch[1]); $left = $lMatch[2]; } else { $lTest = 0; } if(preg_match('/^([0-9]+)([^0-9].*)?$/Us', $right, $rMatch)) { $rTest = intval($rMatch[1]); $right = $rMatch[2]; } else { $rTest = 0; } $test = $lTest - $rTest; if($test != 0) { return $test; } } return strcmp($left, $right); } ?> The code is not optimized. It was just made to solve my problem. ---- Server IP: 216.194.113.175 Probable Submitter: 84.55.97.4 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.strnatcasecmp.php Edit -- http://master.php.net/note/edit/68380 Del: integrated -- http://master.php.net/note/delete/68380/integrated Del: useless -- http://master.php.net/note/delete/68380/useless Del: bad code -- http://master.php.net/note/delete/68380/bad+code Del: spam -- http://master.php.net/note/delete/68380/spam Del: non-english -- http://master.php.net/note/delete/68380/non-english Del: in docs -- http://master.php.net/note/delete/68380/in+docs Del: other reasons-- http://master.php.net/note/delete/68380 Reject -- http://master.php.net/note/reject/68380 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#115117) next »