summaryrefslogtreecommitdiff
path: root/include/utf8/str_pad.php
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2019-11-17 20:57:39 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2019-11-17 20:57:39 +0100
commit3b06ee0d381dc1be5f40ca98ad4278046d869d21 (patch)
treed31e79fc57d882b8267f40c3434480bb58a3ca73 /include/utf8/str_pad.php
downloadfluxbb-master.tar.xz
checked in initial customized verison for Archlinux32HEADmaster
Diffstat (limited to 'include/utf8/str_pad.php')
-rw-r--r--include/utf8/str_pad.php59
1 files changed, 59 insertions, 0 deletions
diff --git a/include/utf8/str_pad.php b/include/utf8/str_pad.php
new file mode 100644
index 0000000..93a559a
--- /dev/null
+++ b/include/utf8/str_pad.php
@@ -0,0 +1,59 @@
+<?php
+
+/**
+* @version $Id: str_pad.php,v 1.1 2006/09/03 09:25:13 harryf Exp $
+* @package utf8
+* @subpackage strings
+*/
+
+/**
+* Replacement for str_pad. $padStr may contain multi-byte characters.
+*
+* @author Oliver Saunders <oliver (a) osinternetservices.com>
+* @param string $input
+* @param int $length
+* @param string $padStr
+* @param int $type ( same constants as str_pad )
+* @return string
+* @see http://www.php.net/str_pad
+* @see utf8_substr
+* @package utf8
+* @subpackage strings
+*/
+function utf8_str_pad($input, $length, $padStr=' ', $type=STR_PAD_RIGHT)
+{
+ $inputLen = utf8_strlen($input);
+ if ($length <= $inputLen)
+ return $input;
+
+ $padStrLen = utf8_strlen($padStr);
+ $padLen = $length - $inputLen;
+
+ if ($type == STR_PAD_RIGHT)
+ {
+ $repeatTimes = ceil($padLen / $padStrLen);
+ return utf8_substr($input.str_repeat($padStr, $repeatTimes), 0, $length);
+ }
+
+ if ($type == STR_PAD_LEFT)
+ {
+ $repeatTimes = ceil($padLen / $padStrLen);
+ return utf8_substr(str_repeat($padStr, $repeatTimes), 0, floor($padLen)).$input;
+ }
+
+ if ($type == STR_PAD_BOTH)
+ {
+ $padLen /= 2;
+ $padAmountLeft = floor($padLen);
+ $padAmountRight = ceil($padLen);
+ $repeatTimesLeft = ceil($padAmountLeft / $padStrLen);
+ $repeatTimesRight = ceil($padAmountRight / $padStrLen);
+
+ $paddingLeft = utf8_substr(str_repeat($padStr, $repeatTimesLeft), 0, $padAmountLeft);
+ $paddingRight = utf8_substr(str_repeat($padStr, $repeatTimesRight), 0, $padAmountLeft);
+
+ return $paddingLeft.$input.$paddingRight;
+ }
+
+ trigger_error('utf8_str_pad: Unknown padding type ('.$type.')', E_USER_ERROR);
+}