php中文字串截取 php截取中文字串的函數

來源:互聯網
上載者:User
  1. /**
  2. @中文字串截取函數
  3. @http://bbs.it-home.org
  4. */
  5. function cnSubStr($string,$sublen)
  6. {
  7. if($sublen>=strlen($string))
  8. {
  9. return $string;
  10. }
  11. $s="";
  12. for($i=0;$i<$sublen;$i++)
  13. {
  14. if(ord($string{$i})>127)
  15. {
  16. $s.=$string{$i}.$string{++$i};
  17. continue;
  18. }else{
  19. $s.=$string{$i};
  20. continue;
  21. }
  22. }
  23. return $s;
  24. }// End Function cnSubStr($string,$sublen)
  25. /////////// Use like this :
  26. echo "

    __________________________

    ";

  27. $string="382指令碼學堂8923專業網站技術網站89023";
  28. $sublen=strlen($string);
  29. $len=20;
  30. echo $string."

    ";

  31. echo "總長為:".($sublen+1)."

    ";

  32. echo "截取數:".$len."

    ";

  33. for($i=1;$i<=$sublen+1;$i++){
  34. if($i>$len){
  35. echo $i." ".cnSubStr($string,$i)."…
    ";
  36. continue;
  37. }
  38. echo $i." ".cnSubStr($string,$i)."
    ";
  39. }
  40. ?>
複製代碼
  • 聯繫我們

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