Jump to content
php.lv forumi

array sort latvian


waplet

Recommended Posts

$string = "glāžšķūņa rūķīš";
mb_internal_encoding("UTF-8");
var_dump(setlocale(LC_COLLATE,"Latvian_Latvia.1257"));

sort($letters, SORT_LOCALE_STRING);

 

a, š, ī, ū, ķ, ņ, ā, ž, g, l, r,

izvada šādi.

OS: Win 7 Ultimate 32 bit

Regional un pāŗejais: Latvian Latvia un tml..

Ko darīt, lai pareizi kārto?

Link to comment
Share on other sites

Katram gadījumam, vai ir kāds īsāks pierakst , kā šis?

function latvian_ord($char) 
     {
       static $alf_flip=array('1'=>-9,'2'=>-8,'3'=>-7,'4'=>-6,'5'=>-5,'6'=>-4,'7'=>-3,'8'=>-2,'9'=>-1,'a'=>0, 'ā'=>1, 'b'=>2, 'c'=>3, 'č'=>4, 'd'=>5, 'e'=>6, 'ē'=>7, 'f'=>8, 'g'=>9, 'ģ'=>10, 'h'=>11, 'i'=>12, 'ī'=>13, 'j'=>14, 'k'=>15, 'ķ'=>16, 'l'=>17, 'ļ'=>18, 'm'=>19, 'n'=>20, 'ņ'=>21, 'o'=>22, 'p'=>23, 'r'=>24, 's'=>25, 'š'=>26, 't'=>27, 'u'=>28, 'ū'=>29, 'v'=>30, 'z'=>31, 'ž'=>32);
       if (isset($alf_flip[$char])) return $alf_flip[$char];
       else return -10;   
     }

 function compare_strings_lv($str1,$str2) { 
   $len=max(mb_strlen($str1),mb_strlen($str2));
   if ($str1==$str2) return 0;
   for ($i=0;$i<$len;$i++) {
       if ((mb_substr($str1,$i,1))=='')
           return -1;
       if ((mb_substr($str2,$i,1))=='')
           return 1;
       $str1=mb_strtolower($str1);
       $str2=mb_strtolower($str2);   
       if (latvian_ord(mb_substr($str1,$i,1))>latvian_ord(mb_substr($str2,$i,1)))
       {
           return 1;    
       } else if (latvian_ord(mb_substr($str1,$i,1))<latvian_ord(mb_substr($str2,$i,1)))
           return -1;    

   }   

 }
 usort($letters, 'compare_strings_lv');

Link to comment
Share on other sites

Hmm...ja sapratu kur tā vaina, skaties noderīgajās funkcijās. Man lieka, ka tur bija funkcijas utf2win un win2utf mūsmāju kadriem.

EDIT: vispār ne to sapratu, sortēt ir pavisam cits kas.

Īstenībā šaubos vai tāda funkcija ir, būs vien pašam jāštuko.

Masīvs ar alfabētu, teksta čarus sapin ar indeksiem, sazortē un samauc apakaļ citā masīvā jau sakārtotu.

Nu tā kautkā.

Edited by mounkuls
Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...