Re: [Tiki-devel] A Unicode-aware replacement for the natcasesort() function
Volker Wysk <post-hhF2Jplw28UoZk/[email protected]>
| Newsgroups | gmane.comp.cms.tiki.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi. Here's the next, probably final, version of the mb_natcasesort() function. It should be placed somewhere in the Tiki libraries. I'm not sure where. There's no mb_natsort() function, because natural languages don't order strings that way. See the comment at the head of the code. See the attachment. Volker _______________________________________________ TikiWiki-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/tikiwiki-devel
sort-nat-loc.php
(application/x-php, 2.3 KB)
<?php
// Do a natural, locale aware and case-insenstitive sort of an array of strings.
//
// "Natural" means that numbers inside the strings are sorted by their value, not by the digits they contain. For
// instance, "10" is ordered after "2".
//
// When the "intl" PHP extension is available, the sort is done Unicode aware. The "$lang" argument is the language
// for which to order. Accented characters, like "ä", are ordered like their non-accented counterpart, like "a".
// For the "$lang" argument, "false" can be specified. In this case, the function investigates the language of the
// current user and uses that.
//
// When intl isn't available, mb_natcasesort() function falls back to the the natcasesort() function. This one
// isn't Unicode aware. Accented characters get ordered at the end. The "$lang" argument is ignored in this case.
//
// In case of success, true is returned. In case of an error, false is returned. This are the return values of the
// collator_asort() function from the intl extension. When the "intl" extension isn't available, the result always
// is "true".
//
// The natsort() function orders all capital letters before all lower-case letters. There's no Unicode aware
// counterpart, because natural languages appear to always order case-insensitively. The Collator class is like
// this. So we can't have a mb_natsort() function.
function mb_natcasesort(string $lang, array &$array): bool
{
if (extension_loaded("intl")) {
if ($lang == false) {
$tikilib = TikiLib::lib('tiki');
$loginlib = TikiLib::lib('login');
$user = $loginlib->getUser();
$lang = $tikilib->get_language($user);
}
$coll = collator_create($lang);
collator_set_attribute($coll, Collator::NUMERIC_COLLATION, Collator::ON);
collator_set_attribute($coll, Collator::CASE_FIRST, Collator::LOWER_FIRST);
collator_set_attribute($coll, Collator::ALTERNATE_HANDLING, Collator::SHIFTED);
return collator_asort($coll, $array);
} else {
natcasesort($array); // always returns true
return true;
}
}
// Test
$arr = array("BBB", "bbb", "ß", "abb", "AAA", "äba", "xx100xx", "xx99xx", '"abc"', '_abc');
mb_natcasesort("de_DE", $arr);
//mb_natcasesort(false, $arr);
print_r($arr);
signature.asc
(application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6QXGh82Ov3+2nrxp+K4ydFOsHoUFAmSpanUACgkQ+K4ydFOs HoX6hRAAoit8XRqoCghAm5wCDoGvkzZT5GnO8gQtxJ0M9e6m8/Kqf+I1YxaZTnAB jG9MheAlIXtuhcrxYvfuf3Z4pLxYhXehxTN7oljz1+usNmEpD8E7lXbDV2lKLg+I eKbEL9TEhf/luAOJJluVbHJo+vgLe7IkCP3rJR7pmOC5nqGBRx7Cmx/u9zENDpRW bqm+rxrGZBJRpVgFjPMGxfELmC7PF/DrL68zu7Hv3Oltvmvl3f1e+bdAtB1XlWl7 smenj1SNyLrr/k1TN2yZMPMEhKCEsHNR0nIO8Ts8OARZmcE95rAmkYalY5QSvXBT uBvLNYEbNKUo7tfvU6lCDwriz7i/JpsMLJfo17i2f4x7SnzzKXVSTaiPBInA1A8X fWFIEZe5ZKLPPz0PBIjE8cp/xjFWJ4yv/UGLK4vBF+kg24yJ2NshiffNTwBCBcf3 2ZiEQM8RqSXSQGnJRdNo+dnQ9IdGzmcK9Nni75C5pfcIUE4KvvfxKUFnF94HN49A 5W/EKB/kW29veiVxzdKli8zX7E9IblHK/v8IsLrjN/I2xcDAS4wgcHPW6BJMrl2+ QaT0DngBbKbuUzj487Iq80QN7fccMNxt5IMNHy6xeOUy1nwt4E1lSPS4iMvNsxNh oj0joR/reUurOESsyxoIxgalElrfn/U/0JHmyNROE/zQVO8srAQ= =WzHC -----END PGP SIGNATURE-----