php對UTF8字型串進行單字分割返回數組

來源:互聯網
上載者:User

標籤:blog   http   使用   for   div   .net   

      在網上查了很多字串分割方法,都無法正確對UTF8字串進行正確分割返回單個字元的數組。經過對FTU8編碼的分析寫出了下面的方法對UTF8進行分割。本人測試可用。本方法只支援UTF8編碼的,其它編碼轉自行轉換成UT8再使用。

$tempaddtext="http://www.jishubu.net php對UTF8字型串進行單字分割返回數組";
//$tempaddtext=iconv("GBK","UTF-8",$tempaddtext); //字元編碼轉換,自行判定需要不需要
$cind = 0;$arr_cont = array();for ($i = 0; $i < strlen($tempaddtext); $i++) { if (strlen(substr($tempaddtext, $cind, 1)) > 0) { if (ord(substr($tempaddtext, $cind, 1)) < 192) { if (substr($tempaddtext, $cind, 1) != " ") { array_push($arr_cont, substr($tempaddtext, $cind, 1)); } $cind++; } elseif(ord(substr($tempaddtext, $cind, 1)) < 224) { array_push($arr_cont, substr($tempaddtext, $cind, 2)); $cind+=2; } else { array_push($arr_cont, substr($tempaddtext, $cind, 3)); $cind+=3; } } } print_r($arr_cont);

  返回結果:

Array ( [0] => h [1] => t [2] => t [3] => p [4] => : [5] => / [6] => / [7] => w [8] => w [9] => w [10] => . [11] => j [12] => i [13] => s [14] => h [15] => u [16] => b [17] => u [18] => . [19] => n [20] => e [21] => t [22] => p [23] => h [24] => p [25] => 對 [26] => U [27] => T [28] => F [29] => 8 [30] => 字 [31] => 體 [32] => 串 [33] => 進 [34] => 行 [35] => 單 [36] => 字 [37] => 分 [38] => 割 [39] => 返 [40] => 回 [41] => 數 [42] => 組 )

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.