dtrphb5597 2013-12-11 13:56
浏览 63
已采纳

PHP | 按字母顺序排列的UTF-8标签(未找到“Collat​​or'类)

Edited question. Validated after 18.Dec.2013.

after more search on SO and google, I decided that probably I need to utilize collator_create and collator_sort functions since my site's language is Turkish and I use UTF-8 encoding.

new position where I am is:

/* fetch values */
$etiket_bulutu = '';
while ($beyan->fetch()) 
{
    $etiket_bulutu .=  $tags.', ';
}

$etiket_bulutu = substr_replace($etiket_bulutu ,'',-2); //omit last {, } chars

$etiketler = explode(", ", $etiket_bulutu); //get each tag as arr

$etiketler = array_unique($etiketler);
$etiketler = array_values($etiketler); //only unique tags without NULLs

$etadet = count($etiketler);

$coll = collator_create('tr_TR'); //from http://www.php.net/manual/en/collator.sort.php
collator_sort($coll, $etiketler);

for($x=0;$x<$etadet;$x++)
 {
  echo $etiketler[$x];
  echo "<br />";
 }

Now, I have no html output and in my error.log file I got this warning "... PHP Warning: sort() expects parameter 2 to be long, string given in ..." and this fatal error "PHP Fatal error: Class 'Collator' not found"

I also googled for these issues but couldn't solve my issue. My PHP version is 5.3.24 waiting for your valuable help. BR

question until 18.Dec.2013 is below. this is old ver. of my Q.

my input

in my related MySql table, my each article (each table row) has its tags in row's related cell. my tagging format has 2 rules:

  1. seperate tags using comma and 1 space character (example: tag1, tag2)
  2. if tag has more than 1 word then seperate the words with dash symbol (-).(example: smart-phone, tag2)

as last input, different articles can have same tags of course

my aim

having a page titled "tags", I try to achieve alphabetical sort of all tags and print them with links back to the list of related articles.

I thought the procedure below:

  1. get all tags from table and cretae an array which consists of unique tags.
  2. execute alphabetical sort
  3. visit each element of last form of array to print the tag with the related link

what I tried

note: convert_one_row is a string validation function.



if ($beyan = $db_baglanti->prepare($sorgum)) { /* bind parameters */ $beyan -> bind_param("s", $bindparametre1);

/* execute statement */
$beyan->execute();

/* bind result variables */
$beyan->bind_result($etiketler);

echo "\t".''." "; echo "\t\t".'

'.'Etiketler'.'

'." ";

/* fetch values */
$etiket_bulutu = '';
while ($beyan->fetch()) 
{
    $etiket_bulutu .=  $etiketler.', ';
}

$etiket_bulutu = substr_replace($etiket_bulutu ,'',-2); //en son 2karakteri yani {, } attık $etiket_bulutu = explode(", ", $etiket_bulutu); //her bir etiketten array yarattık sort($etiket_bulutu, SORT_NATURAL | SORT_FLAG_CASE); $etiket_bulutu = array_unique($etiket_bulutu, SORT_STRING); //sadece tekil etiketler kaldı $bulut = ''; foreach($etiket_bulutu as $etiket) {$bulut .= ''.convert_one_row($etiket).', ';} $bulut = substr_replace($bulut ,'',-2); //en son 2karakteri yani {, } attık echo " ".'

'.$bulut.'

'." ";

echo "\t".''." ";
/* close statement */ $beyan->close(); } ?>

my current result

I achieved to print tags with related links however alphabetical sort issue has failed. Currently, 1st article's tags are written then 2nd article's tags, 3rd and so on... But there is no alphabetical sorting.

current situation example:

article 1 tags: table, apple, smart-phone

article 2 tags: smart-phone, world, soccer

current situation: table, apple, smart-phone, world, soccer

what I require: apple, smart-phone, soccer, table

Can you please correct me thank you in advance

  • 写回答

2条回答 默认 最新

  • dongya2578 2013-12-18 14:02
    关注

    After asking this question and learning the answer of it, I needed to find a way to sort UTF-8 (Turkish input) string array from MySql db alphabetically.

    then I used Fy-'s answer in this link and it worked.

    Now, my solution is:

    setlocale(LC_COLLATE, 'tr_TR.utf8'); //utf-8 sorting için gerekli
    $sorgum = "...";
    $bindparametre1 = '...';
    
    if ($beyan = $db_baglanti->prepare($sorgum)) 
    {
        /* bind parameters */
        $beyan -> bind_param("s", $bindparametre1);
    
        /* execute statement */
        $beyan->execute();
    
        /* bind result variables */
        $beyan->bind_result($etiketler);
    
    echo "\t".'<div class="sol-icerik-kapsar">'."
    ";
    echo "\t\t".'<h1>'.'Etiketler'.'</h1>'."
    ";
    
        /* fetch values */
        $etiket_bulutu = '';
        while ($beyan->fetch()) 
        {
            $etiket_bulutu .=  $etiketler.', ';
        }
    $etiket_bulutu = substr_replace($etiket_bulutu ,'',-2); //en son 2karakteri yani {, } attık
    $etiketler = explode(", ", $etiket_bulutu); //her bir etiketten array yarattık
    $etiketler = array_unique($etiketler);
    $etiketler = array_values($etiketler); //sadece tekil etiketler kaldı
    $etadet = count($etiketler);
    
    usort($etiketler, 'strcoll');
    
    echo "
    \t\t".'<p>'."
    ";
    for($x=0;$x<$etadet;$x++)
      {
      echo "\t\t\t".'<a href="'.url_validate(sitenin_koku.'etiketler/'.$etiketler[$x]).'">'.convert_one_row($etiketler[$x]).'</a>, '."
    ";
      }
    echo "\t\t".'</p>'."
    ";
    

    key codes for my solution here are : setlocale(LC_COLLATE, 'tr_TR.utf8'); and usort($etiketler, 'strcoll');

    regards

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀