php mb_chunk_split函數支援寬字元分割

來源:互聯網
上載者:User
  1. /**
  2. * 分割字串
  3. * @param String $str 要分割的字串
  4. * @param int $length 指定的長度
  5. * @param String $end 在分割後的字串塊追加的內容
  6. */
  7. function mb_chunk_split($string, $length, $end, $once = false){
  8. $string = iconv('gb2312', 'utf-8//ignore', $string);
  9. $array = array();
  10. $strlen = mb_strlen($string);
  11. while($strlen){
  12. $array[] = mb_substr($string, 0, $length, "utf-8");
  13. if($once)
  14. return $array[0] . $end;
  15. $string = mb_substr($string, $length, $strlen, "utf-8");
  16. $strlen = mb_strlen($string);
  17. }
  18. return implode($end, $array);
  19. }
  20. $str = 's六一馬上$就dfs要到$@#了';
  21. $str1 = 'aabbccddeefff';
  22. echo mb_chunk_split($str, 3, '...', true); //s六一...馬上$...就df...s要到...$@#...了
  23. echo "
    ";
  24. echo mb_chunk_split($str1, 2, '...'); //aa...bb...cc...dd...ee...ff...f
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.