Merge branch 'maint/7.0'
[ninja.git] / system / core / utf8 / str_pad.php
blob63e27f0eeee65f0fcbb99adfdd438b4c6dc1f530
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * utf8::str_pad
5 * @package Core
6 * @author Kohana Team
7 * @copyright (c) 2007 Kohana Team
8 * @copyright (c) 2005 Harry Fuecks
9 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
11 function _str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
13 if (utf8::is_ascii($str) AND utf8::is_ascii($pad_str))
15 return str_pad($str, $final_str_length, $pad_str, $pad_type);
18 $str_length = utf8::strlen($str);
20 if ($final_str_length <= 0 OR $final_str_length <= $str_length)
22 return $str;
25 $pad_str_length = utf8::strlen($pad_str);
26 $pad_length = $final_str_length - $str_length;
28 if ($pad_type == STR_PAD_RIGHT)
30 $repeat = ceil($pad_length / $pad_str_length);
31 return utf8::substr($str.str_repeat($pad_str, $repeat), 0, $final_str_length);
34 if ($pad_type == STR_PAD_LEFT)
36 $repeat = ceil($pad_length / $pad_str_length);
37 return utf8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)).$str;
40 if ($pad_type == STR_PAD_BOTH)
42 $pad_length /= 2;
43 $pad_length_left = floor($pad_length);
44 $pad_length_right = ceil($pad_length);
45 $repeat_left = ceil($pad_length_left / $pad_str_length);
46 $repeat_right = ceil($pad_length_right / $pad_str_length);
48 $pad_left = utf8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
49 $pad_right = utf8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_left);
50 return $pad_left.$str.$pad_right;
53 trigger_error('utf8::str_pad: Unknown padding type (' . $type . ')', E_USER_ERROR);