PHP string interception method Daquan php intercept string four examples

Source: Internet
Author: User
    1. Intercept Chinese strings
    2. function Mysubstr ($str, $start, $len) {
    3. $tmpstr = "";
    4. $strlen = $start + $len;
    5. for ($i = 0; $i < $strlen; $i + +) {
    6. if (Ord (substr ($str, $i, 1))--0xa0) {
    7. $tmpstr. = substr ($str, $i, 2);
    8. $i + +;
    9. } else
    10. $tmpstr. = substr ($str, $i, 1);
    11. }
    12. return $tmpstr;
    13. }
    14. ?>
Copy Code

2. Intercept UTF8 encoded multibyte string

    1. Intercept UTF8 string
    2. function Utf8substr ($str, $from, $len) {
    3. Return Preg_replace (' #^ (?: [\x00-\x7f]|[ \xc0-\xff][\x80-\xbf]+) {0, '. $from. '} '. ' (?: [\x00-\x7f]| [\xc0-\xff] [\x80-\xbf]+) {0, '. $len. '}). * #s ', ' $ ', $str);
    4. }?>
Copy Code

3. Chinese character interception function supported by UTF-8 and GB2312

  1. /*
  2. Utf-8, gb2312 are supported by the Chinese character interception function Cut_str (string, intercept length, start length, encoding); Encoding defaults to utf-8 start length defaults to 0
  3. */
  4. function Cut_str ($string, $sublen, $start = 0, $code = ' UTF-8 ') {
  5. if ($code = = ' UTF-8 ') {
  6. $pa = "/[\x01-\x7f]| [\XC2-\XDF] [\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]| [\xe1-\xef] [\X80-\XBF] [\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]| [\xf1-\xf7] [\X80-\XBF] [\X80-\XBF] [\x80-\xbf]/];
  7. Preg_match_all ($pa, $string, $t _string);
  8. if (count ($t _string[0])-$start--and $sublen) return join ("', Array_slice ($t _string[0], $start, $sublen))." ...";
  9. return join ("', Array_slice ($t _string[0], $start, $sublen));
  10. }
  11. Else
  12. {
  13. $start = $start;
  14. $sublen = $sublen;
  15. $strlen = strlen ($string);
  16. $tmpstr = ";
  17. for ($i =0; $i < $strlen; $i + +) {if ($i >= $start && $i < ($start + $sublen)) {if (Ord. substr ($string, $i, 1)) >129)
  18. {
  19. $tmpstr. = substr ($string, $i, 2);
  20. }
  21. Else
  22. {
  23. $tmpstr. = substr ($string, $i, 1);
  24. }
  25. }
  26. if (Ord (substr ($string, $i, 1)) >129) $i + +;
  27. }
  28. if (strlen ($TMPSTR) < $strlen) $tmpstr. = "..."; return $tmpstr; }} $str = "ABCD needs to intercept the string"; Echo Cut_str ($STR, 8, 0, ' gb2312 ');?>
Copy Code

4. Bugfree character intercept function

  1. /**
  2. * @package Bugfree
  3. * @version $Id: functionsmain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss EXP $
  4. *
  5. *
  6. * Return part of a string (Enhance the function substr ())
  7. *
  8. * @author Chunsheng Wang
  9. * @param string $String the string to cut.
  10. * @param int $Length The Length of returned string.
  11. * @param booble $Append whether Append "...": false|true
  12. * @return String The cutted string.
  13. */
  14. function Syssubstr ($String, $Length, $Append = False)
  15. {
  16. if (strlen ($String) <= $Length) {
  17. return $String;
  18. } else {
  19. $I = 0;
  20. while ($I < $Length) {
  21. $StringTMP = substr ($String, $I, 1);
  22. if (Ord ($StringTMP) >=224)
  23. {
  24. $StringTMP = substr ($String, $I, 3);
  25. $I = $I + 3;
  26. }
  27. ElseIf (Ord ($StringTMP) >=192)
  28. {
  29. $StringTMP = substr ($String, $I, 2);
  30. $I = $I + 2;
  31. }
  32. Else
  33. {
  34. $I = $I + 1;
  35. }
  36. $StringLast [] = $StringTMP;
  37. }
  38. $StringLast = Implode ("", $StringLast);
  39. if ($Append)
  40. {
  41. $StringLast. = "...";
  42. }
  43. return $StringLast;
  44. }
  45. }
  46. $String = "codebit.cn-Simple, wonderful, universal";
  47. $Length = "18";
  48. $Append = false;
  49. Echo syssubstr ($String, $Length, $Append);
  50. ?>
Copy Code
  • 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.