diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2013-08-11 22:05:36 +0200 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2013-08-11 22:05:36 +0200 |
| commit | 1d01023d44f2d338b8bbcd28f0830ce813c2595d (patch) | |
| tree | 293f4881d0f1b46371f67a1f4cb3da3efcfd8b7a /src | |
| parent | 0afefd0cc71d0c107edf371a6a3bdf5b3e36652f (diff) | |
| download | auerswald-callnotifier-1d01023d44f2d338b8bbcd28f0830ce813c2595d.tar.gz auerswald-callnotifier-1d01023d44f2d338b8bbcd28f0830ce813c2595d.zip | |
use multibyte functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/callnotifier/Functions.php | 60 | ||||
| -rw-r--r-- | src/callnotifier/Logger/CallFileTop.php | 4 |
2 files changed, 62 insertions, 2 deletions
diff --git a/src/callnotifier/Functions.php b/src/callnotifier/Functions.php new file mode 100644 index 0000000..406317f --- /dev/null +++ b/src/callnotifier/Functions.php @@ -0,0 +1,60 @@ +<?php +namespace callnotifier; + +/** + * Some helper functions + */ +class Functions +{ + /** + * Multibyte-version of str_pad + * + * @link http://stackoverflow.com/a/14773638/282601 + */ + public static function mb_str_pad( + $input, $pad_length, $pad_string = ' ', + $pad_type = STR_PAD_RIGHT, $encoding = 'UTF-8' + ) { + if (!function_exists('mb_substr')) { + return str_pad($input, $pad_length, $pad_string, $pad_type); + } + + $input_length = mb_strlen($input, $encoding); + $pad_string_length = mb_strlen($pad_string, $encoding); + + if ($pad_length <= 0 || ($pad_length - $input_length) <= 0) { + return $input; + } + + $num_pad_chars = $pad_length - $input_length; + + switch ($pad_type) { + case STR_PAD_RIGHT: + $left_pad = 0; + $right_pad = $num_pad_chars; + break; + + case STR_PAD_LEFT: + $left_pad = $num_pad_chars; + $right_pad = 0; + break; + + case STR_PAD_BOTH: + $left_pad = floor($num_pad_chars / 2); + $right_pad = $num_pad_chars - $left_pad; + break; + } + + $result = ''; + for ($i = 0; $i < $left_pad; ++$i) { + $result .= mb_substr($pad_string, $i % $pad_string_length, 1, $encoding); + } + $result .= $input; + for ($i = 0; $i < $right_pad; ++$i) { + $result .= mb_substr($pad_string, $i % $pad_string_length, 1, $encoding); + } + + return $result; + } +} +?> diff --git a/src/callnotifier/Logger/CallFileTop.php b/src/callnotifier/Logger/CallFileTop.php index 1412782..fca02ab 100644 --- a/src/callnotifier/Logger/CallFileTop.php +++ b/src/callnotifier/Logger/CallFileTop.php @@ -99,9 +99,9 @@ class Logger_CallFileTop extends Logger_CallBase if ($this->callTypes == 'io') { $str .= $prefix; - $str .= str_pad($numstr, 20); + $str .= Functions::mb_str_pad($numstr, 20); } else { - $str .= ' ' . str_pad($numstr, 25); + $str .= ' ' . Functions::mb_str_pad($numstr, 25); } $str .= ' ' . date('H:i:s', $call->end - $call->start - 3600); |
