PSR에 맞게 수정
This commit is contained in:
+71
-54
@@ -1,20 +1,22 @@
|
||||
<?php
|
||||
namespace sammo;
|
||||
|
||||
class StringUtil {
|
||||
public static function GetStrLen($str) {
|
||||
class StringUtil
|
||||
{
|
||||
public static function GetStrLen($str)
|
||||
{
|
||||
$count = strlen($str);
|
||||
$len = 0;
|
||||
for($i=0; $i < $count; ) {
|
||||
for ($i=0; $i < $count;) {
|
||||
$code = ord($str[$i]);
|
||||
|
||||
if($code >= 0xf0) {
|
||||
if ($code >= 0xf0) {
|
||||
$len++;
|
||||
$i += 4;
|
||||
} elseif($code >= 0xe0) {
|
||||
} elseif ($code >= 0xe0) {
|
||||
$len++;
|
||||
$i += 3;
|
||||
} elseif($code >= 0xc2) {
|
||||
} elseif ($code >= 0xc2) {
|
||||
$len++;
|
||||
$i += 2;
|
||||
} else {
|
||||
@@ -25,29 +27,32 @@ class StringUtil {
|
||||
return $len;
|
||||
}
|
||||
|
||||
public static function SubStr($str, $s, $l=1000) {
|
||||
public static function SubStr($str, $s, $l=1000)
|
||||
{
|
||||
$count = strlen($str);
|
||||
$startByte = 0; $isSet = 0;
|
||||
$startByte = 0;
|
||||
$isSet = 0;
|
||||
$endByte = $count;
|
||||
$len = 0;
|
||||
for($i=0; $i < $count; ) {
|
||||
for ($i=0; $i < $count;) {
|
||||
$code = ord($str[$i]);
|
||||
|
||||
if($isSet == 0 && $len >= $s) {
|
||||
$startByte = $i; $isSet = 1;
|
||||
if ($isSet == 0 && $len >= $s) {
|
||||
$startByte = $i;
|
||||
$isSet = 1;
|
||||
}
|
||||
if($isSet == 1 && $len-$s >= $l) {
|
||||
if ($isSet == 1 && $len-$s >= $l) {
|
||||
$endByte = $i;
|
||||
break;
|
||||
}
|
||||
|
||||
if($code >= 0xf0) {
|
||||
if ($code >= 0xf0) {
|
||||
$len++;
|
||||
$i += 4;
|
||||
} elseif($code >= 0xe0) {
|
||||
} elseif ($code >= 0xe0) {
|
||||
$len++;
|
||||
$i += 3;
|
||||
} elseif($code >= 0xc2) {
|
||||
} elseif ($code >= 0xc2) {
|
||||
$len++;
|
||||
$i += 2;
|
||||
} else {
|
||||
@@ -59,39 +64,43 @@ class StringUtil {
|
||||
return $str;
|
||||
}
|
||||
|
||||
public static function SubStrForWidth($str, $s, $w) {
|
||||
public static function SubStrForWidth($str, $s, $w)
|
||||
{
|
||||
$count = strlen($str);
|
||||
$startByte = 0; $isSet = 0;
|
||||
$endByte = $count; $last = 0;
|
||||
$startByte = 0;
|
||||
$isSet = 0;
|
||||
$endByte = $count;
|
||||
$last = 0;
|
||||
$len = 0;
|
||||
$width = 0;
|
||||
for($i=0; $i < $count; ) {
|
||||
for ($i=0; $i < $count;) {
|
||||
$code = ord($str[$i]);
|
||||
|
||||
if($isSet == 0 && $len >= $s) {
|
||||
$startByte = $i; $isSet = 1;
|
||||
if ($isSet == 0 && $len >= $s) {
|
||||
$startByte = $i;
|
||||
$isSet = 1;
|
||||
$width = 0;
|
||||
}
|
||||
if($isSet == 1 && $width == $w) {
|
||||
if ($isSet == 1 && $width == $w) {
|
||||
$endByte = $i;
|
||||
break;
|
||||
}
|
||||
if($isSet == 1 && $width > $w) {
|
||||
if ($isSet == 1 && $width > $w) {
|
||||
$endByte = $i - $last;
|
||||
break;
|
||||
}
|
||||
|
||||
if($code >= 0xf0) {
|
||||
if ($code >= 0xf0) {
|
||||
$len++;
|
||||
$width += 2;
|
||||
$last = 4;
|
||||
$i += 4;
|
||||
} elseif($code >= 0xe0) {
|
||||
} elseif ($code >= 0xe0) {
|
||||
$len++;
|
||||
$width += 2;
|
||||
$last = 3;
|
||||
$i += 3;
|
||||
} elseif($code >= 0xc2) {
|
||||
} elseif ($code >= 0xc2) {
|
||||
$len++;
|
||||
$width += 2;
|
||||
$last = 2;
|
||||
@@ -107,37 +116,41 @@ class StringUtil {
|
||||
return $str;
|
||||
}
|
||||
|
||||
public static function CutStrForWidth($str, $s, $w, $ch='..') {
|
||||
public static function CutStrForWidth($str, $s, $w, $ch='..')
|
||||
{
|
||||
$isCut = 0;
|
||||
$count = strlen($str);
|
||||
$startByte = 0; $isSet = 0;
|
||||
$endByte = $count; $last = 0;
|
||||
$startByte = 0;
|
||||
$isSet = 0;
|
||||
$endByte = $count;
|
||||
$last = 0;
|
||||
$len = 0;
|
||||
$width = 0;
|
||||
for($i=0; $i < $count; ) {
|
||||
for ($i=0; $i < $count;) {
|
||||
$code = ord($str[$i]);
|
||||
|
||||
if($isSet == 0 && $len >= $s) {
|
||||
$startByte = $i; $isSet = 1;
|
||||
if ($isSet == 0 && $len >= $s) {
|
||||
$startByte = $i;
|
||||
$isSet = 1;
|
||||
$width = 0;
|
||||
}
|
||||
if($isSet == 1 && $width >= $w) {
|
||||
if ($isSet == 1 && $width >= $w) {
|
||||
$endByte = $i - $last;
|
||||
$isCut = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if($code >= 0xf0) {
|
||||
if ($code >= 0xf0) {
|
||||
$len++;
|
||||
$width += 2;
|
||||
$last = 4;
|
||||
$i += 4;
|
||||
} elseif($code >= 0xe0) {
|
||||
} elseif ($code >= 0xe0) {
|
||||
$len++;
|
||||
$width += 2;
|
||||
$last = 3;
|
||||
$i += 3;
|
||||
} elseif($code >= 0xc2) {
|
||||
} elseif ($code >= 0xc2) {
|
||||
$len++;
|
||||
$width += 2;
|
||||
$last = 2;
|
||||
@@ -149,15 +162,16 @@ class StringUtil {
|
||||
$i += 1;
|
||||
}
|
||||
}
|
||||
if($isCut != 0) {
|
||||
if ($isCut != 0) {
|
||||
$str = substr($str, $startByte, $endByte-$startByte) . $ch;
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
|
||||
//중간정렬
|
||||
public static function staticFill($str, $maxsize, $ch) {
|
||||
if(!$str){
|
||||
public static function staticFill($str, $maxsize, $ch)
|
||||
{
|
||||
if (!$str) {
|
||||
$str = '';
|
||||
}
|
||||
$size = strlen($str);
|
||||
@@ -165,18 +179,19 @@ class StringUtil {
|
||||
$count = ($maxsize - $size) / 2;
|
||||
$string = '';
|
||||
|
||||
for($i=0; $i < $count; $i++) {
|
||||
for ($i=0; $i < $count; $i++) {
|
||||
$string = $string.$ch;
|
||||
}
|
||||
$string = $string.$str;
|
||||
for($i=0; $i < $count; $i++) {
|
||||
for ($i=0; $i < $count; $i++) {
|
||||
$string = $string.$ch;
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
public static function Fill($str, $maxsize, $ch) {
|
||||
if(!$str){
|
||||
public static function Fill($str, $maxsize, $ch)
|
||||
{
|
||||
if (!$str) {
|
||||
$str = '';
|
||||
}
|
||||
$size = strlen($str);
|
||||
@@ -184,26 +199,27 @@ class StringUtil {
|
||||
$count = ($maxsize - $size) / 2;
|
||||
|
||||
$string = '';
|
||||
for($i=0; $i < $count; $i++) {
|
||||
for ($i=0; $i < $count; $i++) {
|
||||
$string = $string.$ch;
|
||||
}
|
||||
$string = $string.$str;
|
||||
for($i=0; $i < $count; $i++) {
|
||||
for ($i=0; $i < $count; $i++) {
|
||||
$string = $string.$ch;
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
//우측정렬
|
||||
public static function Fill2($str, $maxsize, $ch='0') {
|
||||
if(!$str){
|
||||
public static function Fill2($str, $maxsize, $ch='0')
|
||||
{
|
||||
if (!$str) {
|
||||
$str = '';
|
||||
}
|
||||
$size = strlen($str);
|
||||
|
||||
$count = ($maxsize - $size);
|
||||
$string = '';
|
||||
for($i=0; $i < $count; $i++) {
|
||||
for ($i=0; $i < $count; $i++) {
|
||||
$string = $string.$ch;
|
||||
}
|
||||
$string = $string.$str;
|
||||
@@ -211,7 +227,8 @@ class StringUtil {
|
||||
return $string;
|
||||
}
|
||||
|
||||
public static function EscapeTag($str) {
|
||||
public static function EscapeTag($str)
|
||||
{
|
||||
$str = htmlspecialchars($str);
|
||||
$str = str_replace(["\r\n", "\r", "\n"], '<br>', $str);
|
||||
// return nl2br(htmlspecialchars($str));
|
||||
@@ -219,13 +236,13 @@ class StringUtil {
|
||||
return $str;
|
||||
}
|
||||
|
||||
public static function removeSpecialCharacter($str) {
|
||||
public static function removeSpecialCharacter($str)
|
||||
{
|
||||
return str_replace([
|
||||
' ', '"', '\'', 'ⓝ', 'ⓜ', 'ⓖ', '\\', '/', '`',
|
||||
'-', '=', '[', ']', ';', ',', '.', '~', '!', '@',
|
||||
'#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
|
||||
' ', '"', '\'', 'ⓝ', 'ⓜ', 'ⓖ', '\\', '/', '`',
|
||||
'-', '=', '[', ']', ';', ',', '.', '~', '!', '@',
|
||||
'#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
|
||||
'|', '{', '}', ':', '', '<', '>', '?', ' '
|
||||
], '', $str);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user