문자열 자르기 버그 수정

This commit is contained in:
2018-04-28 01:26:32 +09:00
parent 084c7a2626
commit 1cfab53472
+7 -2
View File
@@ -27,6 +27,9 @@ class StringUtil
$currentPos = 0; $currentPos = 0;
$currentWidth = 0; $currentWidth = 0;
$rawStart = 0;
$rawWidth = 0;
$strings = static::splitString($str); $strings = static::splitString($str);
foreach($strings as $idx=>$char){ foreach($strings as $idx=>$char){
$charWidth = mb_strwidth($char, 'UTF-8'); $charWidth = mb_strwidth($char, 'UTF-8');
@@ -34,10 +37,11 @@ class StringUtil
break; break;
} }
$currentPos += $charWidth; $currentPos += $charWidth;
$rawStart += strlen($char);
} }
if($currentPos + $width >= $length){ if($currentPos + $width >= $length){
return substr($str, $currentPos); return substr($str, $rawStart);
} }
for($idx = $currentPos; $idx < count($strings); $idx++){ for($idx = $currentPos; $idx < count($strings); $idx++){
@@ -48,9 +52,10 @@ class StringUtil
break; break;
} }
$currentWidth += $charWidth; $currentWidth += $charWidth;
$rawWidth += strlen($char);
} }
return substr($str, $currentPos, $currentWidth); return substr($str, $rawStart, $rawWidth);
} }
public static function cutStringForWidth(string $str, int $width, string $endFill='..') public static function cutStringForWidth(string $str, int $width, string $endFill='..')