--- fenglin/lib/str_process.inc.php 2005/04/27 12:54:57 1.8 +++ fenglin/lib/str_process.inc.php 2025/03/01 09:02:28 1.9 @@ -2,43 +2,32 @@ function split_line($str,$prefix="",$width=76) { - $lines = array(); - $lines_count = 0; - $len = strlen($str); - - $hz_ok = true; - if ($width<=0) return $str; + $result = ""; + $len = mb_strlen($str,'UTF-8'); + $prefix_len = mb_strlen($prefix,'UTF-8'); + $line = $prefix; - $line_count = strlen($line); + $line_len = $prefix_len; for($i=0;$i<$len;$i++) { - $line.=$str[$i]; - $line_count++; - if (ord($str[$i])>127) - { - $hz_ok = (!$hz_ok); - } - if (ord($str[$i])<=127) - { - $hz_ok = true; - } - if ($line_count >= $width && $hz_ok) + $c = mb_substr($str,$i,1,'UTF-8'); + $line.=$c; + + // Each UTF-8 CJK character should use two character length for display + $line_len += (strlen($c) <= 2 ? 1 : 2); + + if ($line_len >= $width) { - $lines[$lines_count++] = $line; + $result .= ($line . "\n"); $line = $prefix; - $line_count = strlen($line); + $line_len = $prefix_len; } } - $lines[$lines_count++] = $line; + $result .= ($line . "\n"); - $result = ""; - for($i=0;$i<$lines_count;$i++) - { - $result.=($lines[$i]."\r\n"); - } return $result; } @@ -63,8 +52,8 @@ function str_b_lines($str,$max_line=1) $lines = explode("\n",str_replace("\r\n","\n",$str)); foreach($lines as $tmp => $line) { - $result .= ($line."\r\n"); - $l += (strlen($line)/256 + 1); + $result .= ($line."\n"); + $l += (mb_strlen($line,'UTF-8')/128 + 1); if ($l >= $max_line) break; }