Merge branch '3.1/release/3.1.4' into 3.1/master
[kohana-core.git] / utf8 / str_pad.php
blobd1f4b016bef364e6c8493528580d13f9952d68dc
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3 * UTF8::str_pad
5 * @package Kohana
6 * @author Kohana Team
7 * @copyright (c) 2007-2011 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))
14 return str_pad($str, $final_str_length, $pad_str, $pad_type);
16 $str_length = UTF8::strlen($str);
18 if ($final_str_length <= 0 OR $final_str_length <= $str_length)
19 return $str;
21 $pad_str_length = UTF8::strlen($pad_str);
22 $pad_length = $final_str_length - $str_length;
24 if ($pad_type == STR_PAD_RIGHT)
26 $repeat = ceil($pad_length / $pad_str_length);
27 return UTF8::substr($str.str_repeat($pad_str, $repeat), 0, $final_str_length);
30 if ($pad_type == STR_PAD_LEFT)
32 $repeat = ceil($pad_length / $pad_str_length);
33 return UTF8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)).$str;
36 if ($pad_type == STR_PAD_BOTH)
38 $pad_length /= 2;
39 $pad_length_left = floor($pad_length);
40 $pad_length_right = ceil($pad_length);
41 $repeat_left = ceil($pad_length_left / $pad_str_length);
42 $repeat_right = ceil($pad_length_right / $pad_str_length);
44 $pad_left = UTF8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
45 $pad_right = UTF8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_right);
46 return $pad_left.$str.$pad_right;
49 trigger_error('UTF8::str_pad: Unknown padding type ('.$pad_type.')', E_USER_ERROR);