--- fenglin/lib/str_process.inc.php 2004/06/25 08:58:04 1.3 +++ fenglin/lib/str_process.inc.php 2025/10/30 11:19:14 1.22 @@ -1,69 +1,127 @@ -127) + $c = $str[$i]; + + // Skip special characters + if ($c == "\r" || $c == "\7") { - $hz_ok = (!$hz_ok); - $en_ok = true; + continue; } - if (ord($str[$i])<=127) + + if ($c == "\n") { - $hz_ok = true; - if ($str[$i] == " ") + if ($lines_count + 1 >= $lines_limit) { - $en_ok = true; + break; } - else + + $result .= ($line . $end_of_line); + $lines_count++; + $line = $prefix; + $line_len = $prefix_len; + continue; + } + + // Process UTF-8 Chinese characters + $v1 = ord($c); + if ($v1 & 0x80) //head of multi-byte character + { + $v2 = ($v1 & 0x70) << 1; + while ($v2 & 0x80) + { + $i++; + $c .= $str[$i]; + $v2 = ($v2 & 0x7f) << 1; + } + + // Each UTF-8 CJK character should use two character length for display + if ($line_len + 2 > $width) { - $en_ok = false; + 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 && $en_ok) + else { - $lines[$lines_count++] = $line; - $line = $pre; - $line_count = strlen($line); + $line_len++; } - } - $lines[$lines_count++] = $line; - $result = ""; - for($i=0;$i<$lines_count;$i++) - { - $result.=($lines[$i]."\r\n"); - } - return $result; -} + if ($line_len > $width) + { + if ($lines_count + 1 >= $lines_limit) + { + break; + } -function str_process($str,$pre="",$width=75) -{ - $result = ""; - - $lines = explode("\n",str_replace("\r\n","\n",$str)); + $result .= ($line . $end_of_line); + $lines_count++; + $line = $prefix; + $line_len = $prefix_len + 1; + } + + $line .= $c; + } - foreach($lines as $line) + if ($lines_count < $lines_limit) { - $result .= split_line($line,$pre,$width); + $result .= $line; } - + return $result; } - -?>