예제 #1
0
파일: intl.php 프로젝트: ak4n/portable-utf8
/**
 * @param string $str
 * @param int    $start
 * @param int    $len
 *
 * @return false|string
 */
function grapheme_substr($str, $start, $len = 2147483647)
{
    return s\Intl::grapheme_substr($str, $start, $len);
}
예제 #2
0
파일: UTF8.php 프로젝트: hhgr/hhgolf
 /**
  * Get part of string
  *
  * @link http://php.net/manual/en/function.mb-substr.php
  *
  * @param string  $str       <p>
  *                           The string being checked.
  *                           </p>
  * @param int     $start     <p>
  *                           The first position used in str.
  *                           </p>
  * @param int     $length    [optional] <p>
  *                           The maximum length of the returned string.
  *                           </p>
  * @param boolean $cleanUtf8 Clean non UTF-8 chars from the string
  *
  * @return string mb_substr returns the portion of
  * str specified by the
  * start and
  * length parameters.
  */
 public static function substr($str, $start = 0, $length = null, $cleanUtf8 = false)
 {
     static $bug62759;
     $str = (string) $str;
     if (!isset($str[0])) {
         return '';
     }
     // init
     self::checkForSupport();
     if ($cleanUtf8 === true) {
         // iconv and mbstring are not tolerant to invalid encoding
         // further, their behaviour is inconsistent with that of PHP's substr
         $str = self::clean($str);
     }
     if ($length === null) {
         $length = (int) self::strlen($str);
     } else {
         $length = (int) $length;
     }
     if (self::$support['mbstring'] === true) {
         return mb_substr($str, $start, $length, 'UTF-8');
     }
     if (self::$support['iconv'] === true) {
         if (!isset($bug62759)) {
             $bug62759 = 'à' === grapheme_substr('éà', 1, -2);
         }
         if ($bug62759) {
             return (string) Intl::grapheme_substr_workaround62759($str, $start, $length);
         } else {
             return (string) grapheme_substr($str, $start, $length);
         }
     }
     // fallback
     // split to array, and remove invalid characters
     $array = self::split($str);
     // extract relevant part, and join to make sting again
     return implode(array_slice($array, $start, $length));
 }