PHP Substr庫函數的功能介紹_PHP教程

來源:互聯網
上載者:User
初學下面這個PHP Substr庫函數程式不算完美,但處理一般的中文(GB18030,GB2312,BIG5)是沒有問題的。這個函數不適合utf-8編碼的文字。

 
  1. //$str字串
  2. //$max 最大字元數
  3. function Substring($str,$max){
  4. $cnt=0; //實際計數
  5. $index=0; //當前索引
  6. $output=''; //輸出
  7. //
  8. while($cnt<$max && $index<strlen($str)){
  9. $output.=$str[$index];
  10. //big5
  11. if(ord($str[$index])>=0x81 &&
    ord($str[$index])<=0xfe){
  12. if($index+1<strlen($str)){
  13. if( (ord($str[$index+1])>=0x40
    && ord($str[$index+1])<0x7e)
  14. || (ord($str[$index+1])>=0xa1
    && ord($str[$index+1])<=0xfe) ){
  15. $index++;
  16. $output.=$str[$index];
  17. }
  18. }
  19. }
  20. //gb2312
  21. else if(ord($str[$index])>=0xa1
    && ord($str[$index])<=0xf7){
  22. $output.=$str[$index];
  23. if($index+1<strlen($str)){
  24. if(ord($str[$index+1])>=0xa1
    && ord($str[$index+1])<0xfe){
  25. $index++;
  26. $output.=$str[$index];
  27. }
  28. }
  29. }
  30. else{
  31. }
  32. $cnt++;
  33. $index++;
  34. }
  35. return $output;
  36. }

以上程式碼範例就是PHP Substr庫函數在截取中文字元時的具體使用方法。


http://www.bkjia.com/PHPjc/446214.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446214.htmlTechArticle初學 下面這個PHP Substr庫函數程式不算完美,但處理一般的中文(GB18030,GB2312,BIG5)是沒有問題的。這個函數不適合utf-8編碼的文字。 //$str字元...

  • 聯繫我們

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