php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38203 strnatcmp and strnatcasecmp do not handle swedish characters correctly
Submitted: 2006-07-25 07:35 UTC Modified: 2008-11-21 16:21 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:3 (100.0%)
From: thomas at uninet dot se Assigned:
Status: Wont fix Package: Strings related
PHP Version: 5.2.6 OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2006-07-25 07:35 UTC] thomas at uninet dot se
Description:
------------
The bug http://bugs.php.net/bug.php?id=19795 is still not fixed. When using swedish characters and sorting them with strnatcmp and strnatcasecmp the result appears in the wrong order.



Reproduce code:
---------------
<?php
function test1($left, $right) {
  return strnatcasecmp($left, $right);
}
function test1b($left, $right) {
  return strcasecmp($left, $right);
}
$names = array('thomas', 'susanne', 'daniel', 'emelie', '?rjan');
setlocale(LC_ALL, 'se_SV');
uasort($names, 'test1');
print_r($names);
uasort($names, 'test1b');
print_r($names);
?>

Expected result:
----------------
Array
(
    [2] => daniel
    [3] => emelie
    [1] => susanne
    [0] => thomas
    [4] => ?rjan
)
Array
(
    [2] => daniel
    [3] => emelie
    [1] => susanne
    [0] => thomas
    [4] => ?rjan
)



Actual result:
--------------
Array
(
    [4] => ?rjan
    [2] => daniel
    [3] => emelie
    [1] => susanne
    [0] => thomas
)
Array
(
    [2] => daniel
    [3] => emelie
    [1] => susanne
    [0] => thomas
    [4] => ?rjan
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-25 07:49 UTC] thomas at uninet dot se
setlocale(LC_ALL, 'se_SV') returns bool(false).

Even if the setlocale doesn't work it still gives a correct response with strcmp and strcasecmp which (I think) indicates that the *nat* functions uses another approach than the other compare functions.
 [2006-07-25 08:03 UTC] thomas at uninet dot se
Added support for sv_SE so setlocale returns "sv_SE" but it still doesn't work.
 [2006-07-26 23:33 UTC] sniper@php.net
This is Andrei's code, according to the notes in the code.
 [2008-11-02 12:28 UTC] jani@php.net
Deassigning since Andrei does not do PHP anymore, it seems.
 [2008-11-21 16:21 UTC] jani@php.net
There's no point in "fixing" this more in PHP_5_2 considering this is so easily addressed by proper localization tool called "intl".
Here's how:

First: "pecl install intl" (and add the extension to be loaded in your php.ini of course :)

Then use this script:

<?php
$coll = collator_create( 'sv_SV' );
$arr = array('thomas', 'susanne', 'daniel', 'emelie', '?rjan');
collator_asort( $coll, $arr, Collator::SORT_STRING );
print_r($arr);
?>

Outputs:

Array
(
    [2] => daniel
    [3] => emelie
    [1] => susanne
    [0] => thomas
    [4] => ?rjan
)



For more info: http://php.net/intl




 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 19:01:28 2024 UTC