3 * @version $Id: str_pad.php,v 1.1 2006/09/03 09:25:13 harryf Exp $
8 //---------------------------------------------------------------
10 * Replacement for str_pad. $padStr may contain multi-byte characters.
12 * @author Oliver Saunders <oliver (a) osinternetservices.com>
13 * @param string $input
15 * @param string $padStr
16 * @param int $type ( same constants as str_pad )
18 * @see http://www.php.net/str_pad
23 function utf8_str_pad($input, $length, $padStr = ' ', $type = STR_PAD_RIGHT) {
25 $inputLen = utf8_strlen($input);
26 if ($length <= $inputLen) {
30 $padStrLen = utf8_strlen($padStr);
31 $padLen = $length - $inputLen;
33 if ($type == STR_PAD_RIGHT) {
34 $repeatTimes = ceil($padLen / $padStrLen);
35 return utf8_substr($input . str_repeat($padStr, $repeatTimes), 0, $length);
38 if ($type == STR_PAD_LEFT) {
39 $repeatTimes = ceil($padLen / $padStrLen);
40 return utf8_substr(str_repeat($padStr, $repeatTimes), 0, floor($padLen)) . $input;
43 if ($type == STR_PAD_BOTH) {
46 $padAmountLeft = floor($padLen);
47 $padAmountRight = ceil($padLen);
48 $repeatTimesLeft = ceil($padAmountLeft / $padStrLen);
49 $repeatTimesRight = ceil($padAmountRight / $padStrLen);
51 $paddingLeft = utf8_substr(str_repeat($padStr, $repeatTimesLeft), 0, $padAmountLeft);
52 $paddingRight = utf8_substr(str_repeat($padStr, $repeatTimesRight), 0, $padAmountLeft);
53 return $paddingLeft . $input . $paddingRight;
56 trigger_error('utf8_str_pad: Unknown padding type (' . $type . ')',E_USER_ERROR);