Example #1
0
 function utf8_substr($str, $start, $len = null)
 {
     if (!strcmp($len, '0')) {
         return '';
     }
     $byte_start = @utf8_char2byte_pos($str, $start);
     if ($byte_start === false) {
         if ($start > 0) {
             return false;
             // $start outside string length
         } else {
             $start = 0;
         }
     }
     $str = substr($str, $byte_start);
     if ($len != null) {
         $byte_end = @utf8_char2byte_pos($str, $len);
         if ($byte_end === false) {
             // $len outside actual string length
             return $len < 0 ? '' : $str;
         } else {
             return substr($str, 0, $byte_end);
         }
     } else {
         return $str;
     }
 }
Example #2
0
 function mb_substr($str, $start, $len = null, $encoding = "utf-8")
 {
     if ($encoding != "utf-8") {
         return -1;
     }
     $byte_start = utf8_char2byte_pos($str, $start);
     if ($byte_start === false) {
         return false;
     }
     // $start outside string length
     $str = substr($str, $byte_start);
     if ($len != null) {
         $byte_end = utf8_char2byte_pos($str, $len);
         if ($byte_end === false) {
             // $len outside actual string length
             return $str;
         } else {
             return substr($str, 0, $byte_end);
         }
     } else {
         return $str;
     }
 }