PHP interception in Chinese and English

Source: Internet
Author: User
The code is as follows:

/** * @param $string * @param $from starting from 0 ... * @param null $length * @r Eturn String * C_SUBSTR ("Give Me a little", $from =0, 9) >> give me A * C_SUBSTR ("Give Me a little", $from =1, 9) >> I * c_subst R ("Give Me a little", $from =2, 9) >> I * C_SUBSTR ("Give Me a little", $from =3, 9) >> me a little * *Private functionc_substr($string, $from=0, $length = null){//Required field checkif(!isset($string)||!isset($from)||$from<0){return''; }//Get string length$strLen= Strlen ($string);//Set length to string if no length is setif($length==NULL){$length=$strLen; }//Determine if the string length is less than or equal to $length, then return the string directlyif($from==0&&$length>=$strLen){return$string; }$subLen=$length+$from;$rtnStr="";//Start intercept string//In order to intercept the complete, you need to traverse from the beginning for($i=0;$i<$subLen&&$i<$strLen;$i++){//To determine if it is Chinese if it is not very long, and then cut back 2 bytesif(Ord ($string[$i]) >0xa0){if($i+2<$subLen&&$i+2<$strLen){$rtnStr.=$string[$i].$string[$i+1].$string[$i+2];/** * If "give me a little" * intercept 9 bytes from position 0, just get "give me One" * intercept 9 bytes from position 1, then only get "I "* Intercept 9 bytes from position 2, get only" i "* intercept 9 bytes from position 3, then get only" me "* the following if is to skip the beginning not Full character * /if($i<$from){$rtnStr=""; }$i+=2; }Else{return$rtnStr; }            }//EnglishElse{$rtnStr.=$string[$i]; }        }return$rtnStr; }

Copyright Notice: Reproduced when the mention of me on the line ...

The above describes the PHP interception in English, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.