StringUtil에 splitString, padString 추가
This commit is contained in:
+61
-37
@@ -168,27 +168,69 @@ class StringUtil
|
||||
return $str;
|
||||
}
|
||||
|
||||
//중간정렬
|
||||
public static function staticFill($str, $maxsize, $ch)
|
||||
{
|
||||
if (!$str) {
|
||||
$str = '';
|
||||
function splitString($str, $l = 0) {
|
||||
//https://php.net/manual/kr/function.str-split.php#107658
|
||||
if ($l > 0) {
|
||||
$ret = array();
|
||||
$len = mb_strlen($str, "UTF-8");
|
||||
for ($i = 0; $i < $len; $i += $l) {
|
||||
$ret[] = mb_substr($str, $i, $l, "UTF-8");
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
$size = strlen($str);
|
||||
|
||||
$count = ($maxsize - $size) / 2;
|
||||
$string = '';
|
||||
|
||||
for ($i=0; $i < $count; $i++) {
|
||||
$string = $string.$ch;
|
||||
}
|
||||
$string = $string.$str;
|
||||
for ($i=0; $i < $count; $i++) {
|
||||
$string = $string.$ch;
|
||||
}
|
||||
return $string;
|
||||
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* str_pad를 유니코드에서 사용할 수 있는 함수, monospace 전각, 반각 구분을 포함.
|
||||
* @param string $str 원본 문자열
|
||||
* @param int $maxsize 채우고자 하는 너비. 전각 문자는 2, 반각 문자는 1을 기준으로 함
|
||||
* @param string $ch 채움 문자열
|
||||
* @param int $position 원본 문자열의 위치. -1:왼쪽(오른쪽을 채움), 0:가운데(양쪽을 채움), 1:오른쪽(왼쪽을 채움)
|
||||
* @return string 채워진 문자열. 완벽히 채울 수 없는 경우 $maxsize보다 살짝 작은 길이로 반환 됨.
|
||||
*/
|
||||
public static function padString(string $str, int $maxsize, string $ch = ' ', int $position = 0){
|
||||
$chLen = mb_strwidth($ch, 'UTF-8');
|
||||
|
||||
if($chLen == 0){
|
||||
return padString($str, $maxsize, ' ', $position);
|
||||
}
|
||||
|
||||
$textLen = mb_strwidth($str, 'UTF-8');
|
||||
|
||||
$fillTextCnt = intdiv($maxsize - $textLen, $chLen);
|
||||
|
||||
if($position < 0){
|
||||
$fillLeftCnt = 0;
|
||||
$fillRightCnt = $fillTextCnt;
|
||||
}
|
||||
else if($position > 0){
|
||||
$fillLeftCnt = $fillTextCnt;
|
||||
$fillRightCnt = 0;
|
||||
}
|
||||
else {
|
||||
$fillLeftCnt = intdiv($fillTextCnt, 2);
|
||||
$fillRightCnt = $fillTextCnt - $fillLeftCnt;
|
||||
}
|
||||
|
||||
return str_repeat($ch, $fillLeftCnt).$str.str_repeat($ch, $fillRightCnt);
|
||||
}
|
||||
|
||||
public static function padStringAlignRight(string $str, int $maxsize, string $ch = ' '){
|
||||
return static::padString($str, $maxsize, $ch, 1);
|
||||
}
|
||||
|
||||
public static function padStringAlignLeft(string $str, int $maxsize, string $ch = ' '){
|
||||
return static::padString($str, $maxsize, $ch, -1);
|
||||
}
|
||||
|
||||
public static function padStringAlignCenter(string $str, int $maxsize, string $ch = ' '){
|
||||
return static::padString($str, $maxsize, $ch, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function Fill($str, $maxsize, $ch)
|
||||
{
|
||||
if (!$str) {
|
||||
@@ -209,24 +251,6 @@ class StringUtil
|
||||
return $string;
|
||||
}
|
||||
|
||||
//우측정렬
|
||||
public static function Fill2($str, $maxsize, $ch='0')
|
||||
{
|
||||
if (!$str) {
|
||||
$str = '';
|
||||
}
|
||||
$size = strlen($str);
|
||||
|
||||
$count = ($maxsize - $size);
|
||||
$string = '';
|
||||
for ($i=0; $i < $count; $i++) {
|
||||
$string = $string.$ch;
|
||||
}
|
||||
$string = $string.$str;
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
public static function EscapeTag($str)
|
||||
{
|
||||
$str = htmlspecialchars($str);
|
||||
|
||||
Reference in New Issue
Block a user