php 漢字截取

來源:互聯網
上載者:User

上一篇筆記中記錄了漢字轉拼音,那麼,如果輸入一個人名“方沖”,我想截取這個人的性,那麼該如何做到呢?

在php上,我們採用如下實現方式:

   1. function subString_UTF8($str, $start, $lenth)   2. {   3.     $len = strlen($str);   4.     $r = array();   5.     $n = 0;   6.     $m = 0;   7.     for($i = 0; $i < $len; $i++) {   8.         $x = substr($str, $i, 1);   9.         $a = base_convert(ord($x), 10, 2);  10.         $a = substr('00000000'.$a, -8);  11.         if ($n < $start){  12.             if (substr($a, 0, 1) == 0) {  13.             }elseif (substr($a, 0, 3) == 110) {  14.                 $i += 1;  15.             }elseif (substr($a, 0, 4) == 1110) {  16.                 $i += 2;  17.             }  18.             $n++;  19.         }else{  20.             if (substr($a, 0, 1) == 0) {  21.                 $r[ ] = substr($str, $i, 1);  22.             }elseif (substr($a, 0, 3) == 110) {  23.                 $r[ ] = substr($str, $i, 2);  24.                 $i += 1;  25.             }elseif (substr($a, 0, 4) == 1110) {  26.                 $r[ ] = substr($str, $i, 3);  27.                 $i += 2;  28.             }else{  29.                 $r[ ] = '';  30.             }  31.             if (++$m >= $lenth){  32.                 break;  33.             }  34.         }  35.     }  36.     return $r;  37. } // End subString_UTF8;

相信大家都學會了易筋經(^_^),練過易筋經的人,學其他武功就非常快了。從易筋經中我們可以領悟到一通則百通的道理,學習中,如果我們可以舉一反三,那技術成長可謂快咯!廢話這麼多,其實就是想說,這個可以可以應用到其他地方,不只限於php

聯繫我們

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