can guarantee word integrity of the PHP English string interception code sharing _php instance

Source: Internet
Author: User

Directly on code:

/** * @param $str * @param $start * @param $length * * @return string/public stat
    IC function Usubstr ($str, $start, $length = null) {//normal intercept first.
    $res = substr ($str, $start, $length);
 
    $strlen = strlen ($STR); /* Then determine whether the 6 bytes are complete (not broken)////If the parameter start is a positive number if ($start >= 0) {//forward and intercept about 6 bytes $next _start = $start + $ Length Initial position $next _len = $next _start + 6 <= $strlen?
      6: $strlen-$next _start;
      $next _segm = substr ($str, $next _start, $next _len); If the 1th byte is not the first byte of the full character, then intercept about 6 bytes $prev _start = $start-6 > 0?
      $start-6:0;
    $prev _segm = substr ($str, $prev _start, $start-$prev _start); //Start is a negative number else {//forward intercept approximately 6 bytes $next _start = $strlen + $start + $length;//initial position $next _len = $ne Xt_start + 6 <= $strlen?
      6: $strlen-$next _start;
 
      $next _segm = substr ($str, $next _start, $next _len);
      If the 1th byte is not the first byte of the full character, then intercept about 6 bytes. $start = $strlen + $start; $prev _start = $start-6 > 0?
      $start-6:0;
    $prev _segm = substr ($str, $prev _start, $start-$prev _start); //Determine if the first 6 bytes conform to the UTF8 rule if (preg_match (' @^ ([x80-xbf]{0,5}) [xc0-xfd]?@ ', $next _segm, $bytes)) {if (!empty ($byt
        Es[1]) {$bytes = $bytes [1];
      $res. = $bytes;
    }//Determine if 6 bytes are compliant with the UTF8 rule $ord 0 = ord ($res [0]);
      if (128 <= $ord 0 && 191 >= $ord 0) {//intercept later and add to the front of Res. if (Preg_match (' @[xc0-xfd][x80-xbf]{0,5}$@ ', $prev _segm, $bytes)) {if (!empty ($bytes [0])) {$bytes = $b
          Ytes[0]; $res = $bytes.
        $res; }} if (Strlen ($res) < $strlen) {$res = $res.
    '...';
  return $res; }

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.