Collect several PHP string intercept functions that support Chinese _php tutorial

Source: Internet
Author: User
String interception is a very common programming task, and often a string interception with Chinese is often used. Although not difficult, but write their own function implementation and time-consuming, here is a more useful string interception function, capable of the basic requirements.

!--? phpfunction syssubstr ($string, $length, $append = False) {if (strlen ($string) <= $length)     {return $string;         } else {$i = 0; while ($i             $length) {$stringTMP = substr ($string, $i, 1); if (Ord ($stringTMP)-->=224)                 {$stringTMP = substr ($string, $i, 3);             $i = $i + 3;                 } elseif (Ord ($stringTMP) >=192) {$stringTMP = substr ($string, $i, 2);             $i = $i + 2;             } else {$i = $i + 1;         } $stringLast [] = $stringTMP;         } $stringLast = Implode ("", $stringLast);         if ($append) {$stringLast. = "...";     } return $stringLast; }} $string = "Help the home-focus on the Internet in the mainstream of a variety of technologies"; $length = "27"; $append = true; Echo syssubstr ($string, $length, $append); Output//Help the home--...? 

Intercept GB2312 Chinese string:

 
  0xa0) {            $tmpstr. = substr ($str, $i, 2);            $i + +;        } else            $tmpstr. = substr ($str, $i, 1);    }    return $tmpstr;}? >

To intercept UTF8 encoded multibyte strings:

 
  

UTF-8, GB2312 are supported by the Chinese character interception function:

 $sublen) return join ("', Array_slice ($t _string[0], $start, $sublen))."         ...";     return join ("', Array_slice ($t _string[0], $start, $sublen));         } else {$start = $start * *;         $sublen = $sublen;         $strlen = strlen ($string);          $tmpstr = ";                 for ($i =0; $i < $strlen; $i + +) {if ($i >= $start && $i < ($start + $sublen)) { if (Ord (substr ($string, $i, 1)) >129) {$tmpstr. = substr ($string, $i,                 2);                 } else {$tmpstr. = substr ($string, $i, 1);         }} if (Ord (substr ($string, $i, 1)) >129) $i + +;         } if (strlen ($TMPSTR) < $strlen) $tmpstr. = "...";     return $tmpstr; }} $str = "ABCD needs to intercept the string"; Echo Cut_str ($STR, 8, 0, ' gb2312 ');?>

http://www.bkjia.com/PHPjc/752356.html www.bkjia.com true http://www.bkjia.com/PHPjc/752356.html techarticle string interception is a very common programming task, and often a string interception with Chinese is often used. It's not hard, but it's time-consuming to write your own functions ...

  • 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.