--- fenglin/lib/str_process.inc.php 2004/06/28 22:55:35 1.4 +++ fenglin/lib/str_process.inc.php 2025/04/04 03:01:37 1.14 @@ -1,59 +1,130 @@ 127) + $c = $str[$i]; + + // Skip special characters + if ($c == "\r" || $c == "\7") + { + continue; + } + + if ($c == "\n") { - $hz_ok = (!$hz_ok); + if ($lines_count + 1 >= $lines_limit) + { + break; + } + + $result .= ($line . $end_of_line); + $lines_count++; + $line = $prefix; + $line_len = $prefix_len; + continue; } - if (ord($str[$i])<=127) + + // Process UTF-8 Chinese characters + $v1 = ord($c); + if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character { - $hz_ok = true; + $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) + { + if ($lines_count + 1 >= $lines_limit) + { + break; + } + + $result .= ($line . $end_of_line); + $lines_count++; + $line = $prefix; + $line_len = $prefix_len; + } + $line_len += 2; } - if ($line_count >= $width && $hz_ok) + else { - $lines[$lines_count++] = $line; + $line_len++; + } + + $line .= $c; + + if ($line_len >= $width) + { + if ($lines_count + 1 >= $lines_limit) + { + break; + } + + $result .= ($line . $end_of_line); + $lines_count++; $line = $prefix; - $line_count = strlen($line); + $line_len = $prefix_len; } } - $lines[$lines_count++] = $line; - $result = ""; - for($i=0;$i<$lines_count;$i++) + if ($lines_count < $lines_limit) { - $result.=($lines[$i]."\r\n"); + $result .= $line; } - return $result; -} -function str_process($str,$prefix="",$width=75) -{ - $result = ""; - - $lines = explode("\n",str_replace("\r\n","\n",$str)); - - foreach($lines as $line) - { - $result .= split_line($line,$prefix,$width); - } - return $result; } - ?>