Beispiel #1
0
/**
 * UTF8::strPad
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strPad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
    if (\Phalcana\UTF8::isAscii($str) && \Phalcana\UTF8::isAscii($pad_str)) {
        return strPad($str, $final_str_length, $pad_str, $pad_type);
    }
    $str_length = \Phalcana\UTF8::strlen($str);
    if ($final_str_length <= 0 || $final_str_length <= $str_length) {
        return $str;
    }
    $pad_str_length = \Phalcana\UTF8::strlen($pad_str);
    $pad_length = $final_str_length - $str_length;
    if ($pad_type == STR_PAD_RIGHT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return \Phalcana\UTF8::substr($str . str_repeat($pad_str, $repeat), 0, $final_str_length);
    }
    if ($pad_type == STR_PAD_LEFT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return \Phalcana\UTF8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)) . $str;
    }
    if ($pad_type == STR_PAD_BOTH) {
        $pad_length /= 2;
        $pad_length_left = floor($pad_length);
        $pad_length_right = ceil($pad_length);
        $repeat_left = ceil($pad_length_left / $pad_str_length);
        $repeat_right = ceil($pad_length_right / $pad_str_length);
        $pad_left = \Phalcana\UTF8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
        $pad_right = \Phalcana\UTF8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_right);
        return $pad_left . $str . $pad_right;
    }
    throw new \Phalcana\Exceptions\UTF8("Phalcana\\UTF8::strPad: Unknown padding type ({$pad_type})");
}
Beispiel #2
0
/**
 * UTF8::strrpos
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strrpos($str, $search, $offset = 0)
{
    $offset = (int) $offset;
    if (\Phalcana\UTF8::isAscii($str) && \Phalcana\UTF8::isAscii($search)) {
        return strrpos($str, $search, $offset);
    }
    if ($offset == 0) {
        $array = explode($search, $str, -1);
        return isset($array[0]) ? \Phalcana\UTF8::strlen(implode($search, $array)) : false;
    }
    $str = \Phalcana\UTF8::substr($str, $offset);
    $pos = \Phalcana\UTF8::strrpos($str, $search);
    return $pos === false ? false : $pos + $offset;
}
Beispiel #3
0
/**
 * UTF8::strcspn
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strcspn($str, $mask, $offset = null, $length = null)
{
    if ($str == '' || $mask == '') {
        return 0;
    }
    if (\Phalcana\UTF8::isAscii($str) && \Phalcana\UTF8::isAscii($mask)) {
        return $offset === null ? strcspn($str, $mask) : ($length === null ? strcspn($str, $mask, $offset) : strcspn($str, $mask, $offset, $length));
    }
    if ($offset !== null || $length !== null) {
        $str = \Phalcana\UTF8::substr($str, $offset, $length);
    }
    // Escape these characters:  - [ ] . : \ ^ /
    // The . and : are escaped to prevent possible warnings about POSIX regex elements
    $mask = preg_replace('#[-[\\].:\\\\^/]#', '\\\\$0', $mask);
    preg_match('/^[^' . $mask . ']+/u', $str, $matches);
    return isset($matches[0]) ? \Phalcana\UTF8::strlen($matches[0]) : 0;
}
Beispiel #4
0
 /**
  * Limits a phrase to a given number of characters.
  *
  *     $text = Text::limitChars($text);
  *
  * @param   string  $str            phrase to limit characters of
  * @param   integer $limit          number of characters to limit to
  * @param   string  $end_char       end character or entity
  * @param   boolean $preserve_words enable or disable the preservation of words while limiting
  * @return  string
  * @uses    UTF8::strlen
  */
 public static function limitChars($str, $limit = 100, $end_char = null, $preserve_words = false)
 {
     $end_char = $end_char === null ? '…' : $end_char;
     $limit = (int) $limit;
     if (trim($str) === '' || UTF8::strlen($str) <= $limit) {
         return $str;
     }
     if ($limit <= 0) {
         return $end_char;
     }
     if ($preserve_words === false) {
         return rtrim(UTF8::substr($str, 0, $limit)) . $end_char;
     }
     // Don't preserve words. The limit is considered the top limit.
     // No strings with a length longer than $limit should be returned.
     if (!preg_match('/^.{0,' . $limit . '}\\s/us', $str, $matches)) {
         return $end_char;
     }
     return rtrim($matches[0]) . (strlen($matches[0]) === strlen($str) ? '' : $end_char);
 }
Beispiel #5
0
 /**
  * Tests UTF8::substr
  *
  * @test
  * @dataProvider provider_substr
  */
 public function test_substr($input, $offset, $length, $expected)
 {
     $this->assertSame($expected, UTF8::substr($input, $offset, $length));
 }