--- fenglin/lib/str_process.inc.php 2025/03/01 09:02:28 1.9 +++ fenglin/lib/str_process.inc.php 2025/03/24 05:46:49 1.11 @@ -1,64 +1,80 @@ = $width) + // Skip special characters + if ($c == "\r" || $c == "\7") + { + continue; + } + + if ($c == "\n") { $result .= ($line . "\n"); + $lines_count++; $line = $prefix; $line_len = $prefix_len; + continue; } - } - $result .= ($line . "\n"); - return $result; -} + // Process UTF-8 Chinese characters + $v1 = ord($c); + if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character + { + $v2 = ($v1 & 0b01111000) << 1; + while ($v2 & 0b10000000) + { + $i++; + $v3 = $str[$i]; + $c .= $v3; + $v2 = ($v2 & 0b01111111 ) << 1; + } + + // Each UTF-8 CJK character should use two character length for display + if ($line_len + 2 > $width) + { + $result .= ($line . "\n"); + $lines_count++; + $line = $prefix; + $line_len = $prefix_len; + } + $line_len += 2; + } + else + { + $line_len++; + } -function str_process($str,$prefix="",$width=76) -{ - $result = ""; - - $lines = explode("\n",str_replace("\r\n","\n",$str)); + $line .= $c; - foreach($lines as $tmp => $line) - { - $result .= split_line($line,$prefix,$width); + if ($line_len >= $width) + { + $result .= ($line . "\n"); + $lines_count++; + $line = $prefix; + $line_len = $prefix_len; + } } - - return $result; -} -function str_b_lines($str,$max_line=1) -{ - $result = ""; - $l = 0; - $lines = explode("\n",str_replace("\r\n","\n",$str)); - foreach($lines as $tmp => $line) + if ($lines_count < $lines_limit) { - $result .= ($line."\n"); - $l += (mb_strlen($line,'UTF-8')/128 + 1); - if ($l >= $max_line) - break; + $result .= $line; } return $result; } - ?>