cutStringForWidth, subStringForWidth 재 구현
This commit is contained in:
+1
-1
@@ -171,7 +171,7 @@ for($j=0; $j < $gencount; $j++) {
|
|||||||
$turn = getTurn($general, 1, 0);
|
$turn = getTurn($general, 1, 0);
|
||||||
|
|
||||||
for($i=0; $i < 5; $i++) {
|
for($i=0; $i < 5; $i++) {
|
||||||
$turn[$i] = StringUtil::SubStrForWidth($turn[$i], 0, 20);
|
$turn[$i] = StringUtil::subStringForWidth($turn[$i], 0, 20);
|
||||||
$k = $i+1;
|
$k = $i+1;
|
||||||
echo "
|
echo "
|
||||||
$k : $turn[$i] <br>";
|
$k : $turn[$i] <br>";
|
||||||
|
|||||||
+3
-3
@@ -33,7 +33,7 @@ if($command == 46) {
|
|||||||
$name = str_replace(" ", "", $name);
|
$name = str_replace(" ", "", $name);
|
||||||
$name = str_replace(" ", "뷁", $name);
|
$name = str_replace(" ", "뷁", $name);
|
||||||
if($name == "") { $name = "무명"; }
|
if($name == "") { $name = "무명"; }
|
||||||
$name = StringUtil::SubStrForWidth($name, 0, 12);
|
$name = StringUtil::subStringForWidth($name, 0, 12);
|
||||||
|
|
||||||
$db->update('general', [
|
$db->update('general', [
|
||||||
'makenation'=>$name
|
'makenation'=>$name
|
||||||
@@ -58,7 +58,7 @@ if($command == 46) {
|
|||||||
$nationname = str_replace(" ", "", $nationname);
|
$nationname = str_replace(" ", "", $nationname);
|
||||||
$nationname = str_replace(" ", "뷁", $nationname);
|
$nationname = str_replace(" ", "뷁", $nationname);
|
||||||
if($nationname == "") { $nationname = "무명"; }
|
if($nationname == "") { $nationname = "무명"; }
|
||||||
$nationname = StringUtil::SubStrForWidth($nationname, 0, 12);
|
$nationname = StringUtil::subStringForWidth($nationname, 0, 12);
|
||||||
|
|
||||||
$query = "update general set makenation='{$nationname}' where level>=5 and nation='{$me['nation']}'";
|
$query = "update general set makenation='{$nationname}' where level>=5 and nation='{$me['nation']}'";
|
||||||
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
||||||
@@ -84,7 +84,7 @@ if($command == 46) {
|
|||||||
$note = str_replace("|", "", $note);
|
$note = str_replace("|", "", $note);
|
||||||
$note = str_replace(" ", "", $note);
|
$note = str_replace(" ", "", $note);
|
||||||
$note = str_replace(" ", "뷁", $note);
|
$note = str_replace(" ", "뷁", $note);
|
||||||
$note = StringUtil::SubStrForWidth($note, 0, 90);
|
$note = StringUtil::subStringForWidth($note, 0, 90);
|
||||||
|
|
||||||
$query = "update diplomacy set reserved='{$note}' where me='{$me['nation']}' and you='$double'";
|
$query = "update diplomacy set reserved='{$note}' where me='{$me['nation']}' and you='$double'";
|
||||||
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
||||||
|
|||||||
+1
-1
@@ -423,7 +423,7 @@ function processAI($no) {
|
|||||||
$colors = GetNationColors();
|
$colors = GetNationColors();
|
||||||
$color = rand() % count($colors);
|
$color = rand() % count($colors);
|
||||||
$command = EncodeCommand(0, $type, $color, 46);
|
$command = EncodeCommand(0, $type, $color, 46);
|
||||||
$nationName = "㉿".StringUtil::SubStr($general['name'], 1);
|
$nationName = "㉿".mb_substr($general['name'], 1);
|
||||||
//건국
|
//건국
|
||||||
$query = "update general set turn0='$command',makenation='$nationName' where no='{$general['no']}'";
|
$query = "update general set turn0='$command',makenation='$nationName' where no='{$general['no']}'";
|
||||||
MYDB_query($query, $connect) or Error("processAI08 ".MYDB_error($connect),"");
|
MYDB_query($query, $connect) or Error("processAI08 ".MYDB_error($connect),"");
|
||||||
|
|||||||
@@ -787,7 +787,7 @@ function process_55(&$general) {
|
|||||||
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
||||||
$nationcount = MYDB_num_rows($result);
|
$nationcount = MYDB_num_rows($result);
|
||||||
|
|
||||||
if($nationcount > 0) { $makename = StringUtil::SubStr("$nationcount".$general['name'], 0, 6); }
|
if($nationcount > 0) { $makename = mb_substr("$nationcount".$general['name'], 0, 6); }
|
||||||
else { $makename = $general['name']; }
|
else { $makename = $general['name']; }
|
||||||
|
|
||||||
if($general['level'] != 0) {
|
if($general['level'] != 0) {
|
||||||
|
|||||||
+3
-3
@@ -400,7 +400,7 @@ function processWar($general, $city) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$render_attacker = [
|
$render_attacker = [
|
||||||
'crewtype' => StringUtil::SubStr(GameUnitConst::byId($general['crewtype'])->name, 0, 2),
|
'crewtype' => mb_substr(GameUnitConst::byId($general['crewtype'])->name, 0, 2),
|
||||||
'name'=> $general['name'],
|
'name'=> $general['name'],
|
||||||
'remain_crew' => $general['crew'],
|
'remain_crew' => $general['crew'],
|
||||||
'killed_crew' => -$mydeathnum
|
'killed_crew' => -$mydeathnum
|
||||||
@@ -1276,13 +1276,13 @@ function processWar($general, $city) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$render_attacker = [
|
$render_attacker = [
|
||||||
'crewtype' => StringUtil::SubStr(GameUnitConst::byId($general['crewtype'])->name, 0, 2),
|
'crewtype' => mb_substr(GameUnitConst::byId($general['crewtype'])->name, 0, 2),
|
||||||
'name'=> $general['name'],
|
'name'=> $general['name'],
|
||||||
'remain_crew' => $general['crew'],
|
'remain_crew' => $general['crew'],
|
||||||
'killed_crew' => -$mydeathnum
|
'killed_crew' => -$mydeathnum
|
||||||
];
|
];
|
||||||
$render_defender = [
|
$render_defender = [
|
||||||
'crewtype' => StringUtil::SubStr(GameUnitConst::byId($oppose['crewtype'])->name, 0, 2),
|
'crewtype' => mb_substr(GameUnitConst::byId($oppose['crewtype'])->name, 0, 2),
|
||||||
'name'=> $oppose['name'],
|
'name'=> $oppose['name'],
|
||||||
'remain_crew' => $oppose['crew'],
|
'remain_crew' => $oppose['crew'],
|
||||||
'killed_crew' => -$opdeathnum
|
'killed_crew' => -$opdeathnum
|
||||||
|
|||||||
+60
-156
@@ -3,169 +3,75 @@ namespace sammo;
|
|||||||
|
|
||||||
class StringUtil
|
class StringUtil
|
||||||
{
|
{
|
||||||
public static function GetStrLen($str)
|
/**
|
||||||
|
* 전각, 반각 길이 기준의 substr
|
||||||
|
* @param string $str 원본 문자열
|
||||||
|
* @param int $start 시작 너비. 일치하는 문자가 없을 경우 그 다음 문자부터. 음수의 경우 뒤에서부터.
|
||||||
|
* @param int|null $width 길이. null일 경우 끝까지.
|
||||||
|
*/
|
||||||
|
public static function subStringForWidth(string $str, int $start = 0, $width = null)
|
||||||
{
|
{
|
||||||
$count = strlen($str);
|
$length = mb_strwidth($str, 'UTF-8');
|
||||||
$len = 0;
|
if($width === null){
|
||||||
for ($i=0; $i < $count;) {
|
$width = $length;
|
||||||
$code = ord($str[$i]);
|
|
||||||
|
|
||||||
if ($code >= 0xf0) {
|
|
||||||
$len++;
|
|
||||||
$i += 4;
|
|
||||||
} elseif ($code >= 0xe0) {
|
|
||||||
$len++;
|
|
||||||
$i += 3;
|
|
||||||
} elseif ($code >= 0xc2) {
|
|
||||||
$len++;
|
|
||||||
$i += 2;
|
|
||||||
} else {
|
|
||||||
$len++;
|
|
||||||
$i += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return $len;
|
|
||||||
|
if($start < 0){
|
||||||
|
$start = $length - $start;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 길이를 직관적으로 알아낼 수 있는 방법은 '없다'.
|
||||||
|
* utf8의 경우 뒤에서부터 세는 것 전략이 가능하나 제외함.
|
||||||
|
*/
|
||||||
|
|
||||||
|
$currentPos = 0;
|
||||||
|
$currentWidth = 0;
|
||||||
|
|
||||||
|
$strings = static::splitString($str);
|
||||||
|
foreach($strings as $idx=>$char){
|
||||||
|
$charWidth = mb_strwidth($char, 'UTF-8');
|
||||||
|
if($currentPos+$charWidth > $start){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$currentPos += $charWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($currentPos + $width >= $length){
|
||||||
|
return substr($str, $currentPos, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
for($idx = $currentPos; $idx < count($strings); $idx++){
|
||||||
|
$char = $strings[$idx];
|
||||||
|
|
||||||
|
$charWidth = mb_strwidth($char, 'UTF-8');
|
||||||
|
if($currentWidth + $charWidth > $width){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$currentWidth += $charWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
return substr($str, $currentPos, $currentWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function SubStr($str, $s, $l=1000)
|
public static function cutStringForWidth(string $str, int $width, string $endFill='..')
|
||||||
{
|
{
|
||||||
$count = strlen($str);
|
if(mb_strwidth($str) <= $width){
|
||||||
$startByte = 0;
|
return $str;
|
||||||
$isSet = 0;
|
}
|
||||||
$endByte = $count;
|
|
||||||
$len = 0;
|
|
||||||
for ($i=0; $i < $count;) {
|
|
||||||
$code = ord($str[$i]);
|
|
||||||
|
|
||||||
if ($isSet == 0 && $len >= $s) {
|
$result = '';
|
||||||
$startByte = $i;
|
$width -= mb_strwidth($endFill, 'UTF-8');
|
||||||
$isSet = 1;
|
|
||||||
}
|
foreach(static::splitString($str) as $char){
|
||||||
if ($isSet == 1 && $len-$s >= $l) {
|
$charWidth = mb_strwidth($char, 'UTF-8');
|
||||||
$endByte = $i;
|
if($charWidth > $width){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
$result .= $char;
|
||||||
if ($code >= 0xf0) {
|
$width -= $charWidth;
|
||||||
$len++;
|
|
||||||
$i += 4;
|
|
||||||
} elseif ($code >= 0xe0) {
|
|
||||||
$len++;
|
|
||||||
$i += 3;
|
|
||||||
} elseif ($code >= 0xc2) {
|
|
||||||
$len++;
|
|
||||||
$i += 2;
|
|
||||||
} else {
|
|
||||||
$len++;
|
|
||||||
$i += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$str = substr($str, $startByte, $endByte-$startByte);
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function SubStrForWidth($str, $s, $w)
|
return $result.$endFill;
|
||||||
{
|
|
||||||
$count = strlen($str);
|
|
||||||
$startByte = 0;
|
|
||||||
$isSet = 0;
|
|
||||||
$endByte = $count;
|
|
||||||
$last = 0;
|
|
||||||
$len = 0;
|
|
||||||
$width = 0;
|
|
||||||
for ($i=0; $i < $count;) {
|
|
||||||
$code = ord($str[$i]);
|
|
||||||
|
|
||||||
if ($isSet == 0 && $len >= $s) {
|
|
||||||
$startByte = $i;
|
|
||||||
$isSet = 1;
|
|
||||||
$width = 0;
|
|
||||||
}
|
|
||||||
if ($isSet == 1 && $width == $w) {
|
|
||||||
$endByte = $i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ($isSet == 1 && $width > $w) {
|
|
||||||
$endByte = $i - $last;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($code >= 0xf0) {
|
|
||||||
$len++;
|
|
||||||
$width += 2;
|
|
||||||
$last = 4;
|
|
||||||
$i += 4;
|
|
||||||
} elseif ($code >= 0xe0) {
|
|
||||||
$len++;
|
|
||||||
$width += 2;
|
|
||||||
$last = 3;
|
|
||||||
$i += 3;
|
|
||||||
} elseif ($code >= 0xc2) {
|
|
||||||
$len++;
|
|
||||||
$width += 2;
|
|
||||||
$last = 2;
|
|
||||||
$i += 2;
|
|
||||||
} else {
|
|
||||||
$len++;
|
|
||||||
$width += 1;
|
|
||||||
$last = 1;
|
|
||||||
$i += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$str = substr($str, $startByte, $endByte-$startByte);
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function CutStrForWidth($str, $s, $w, $ch='..')
|
|
||||||
{
|
|
||||||
$isCut = 0;
|
|
||||||
$count = strlen($str);
|
|
||||||
$startByte = 0;
|
|
||||||
$isSet = 0;
|
|
||||||
$endByte = $count;
|
|
||||||
$last = 0;
|
|
||||||
$len = 0;
|
|
||||||
$width = 0;
|
|
||||||
for ($i=0; $i < $count;) {
|
|
||||||
$code = ord($str[$i]);
|
|
||||||
|
|
||||||
if ($isSet == 0 && $len >= $s) {
|
|
||||||
$startByte = $i;
|
|
||||||
$isSet = 1;
|
|
||||||
$width = 0;
|
|
||||||
}
|
|
||||||
if ($isSet == 1 && $width >= $w) {
|
|
||||||
$endByte = $i - $last;
|
|
||||||
$isCut = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($code >= 0xf0) {
|
|
||||||
$len++;
|
|
||||||
$width += 2;
|
|
||||||
$last = 4;
|
|
||||||
$i += 4;
|
|
||||||
} elseif ($code >= 0xe0) {
|
|
||||||
$len++;
|
|
||||||
$width += 2;
|
|
||||||
$last = 3;
|
|
||||||
$i += 3;
|
|
||||||
} elseif ($code >= 0xc2) {
|
|
||||||
$len++;
|
|
||||||
$width += 2;
|
|
||||||
$last = 2;
|
|
||||||
$i += 2;
|
|
||||||
} else {
|
|
||||||
$len++;
|
|
||||||
$width += 1;
|
|
||||||
$last = 1;
|
|
||||||
$i += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($isCut != 0) {
|
|
||||||
$str = substr($str, $startByte, $endByte-$startByte) . $ch;
|
|
||||||
}
|
|
||||||
return $str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function splitString($str, $l = 0) {
|
function splitString($str, $l = 0) {
|
||||||
@@ -228,12 +134,10 @@ class StringUtil
|
|||||||
return static::padString($str, $maxsize, $ch, 0);
|
return static::padString($str, $maxsize, $ch, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function EscapeTag($str)
|
public static function escapeTag($str)
|
||||||
{
|
{
|
||||||
$str = htmlspecialchars($str);
|
$str = htmlspecialchars($str);
|
||||||
$str = str_replace(["\r\n", "\r", "\n"], '<br>', $str);
|
$str = str_replace(["\r\n", "\r", "\n"], '<br>', $str);
|
||||||
// return nl2br(htmlspecialchars($str));
|
|
||||||
// return htmlspecialchars($str);
|
|
||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user