--- fenglin/lib/str_process.inc.php 2004/06/25 08:26:49 1.2 +++ fenglin/lib/str_process.inc.php 2025/04/07 06:26:44 1.15 @@ -1,55 +1,130 @@ = $width && $hz_ok) + $c = $str[$i]; + + // Process UTF-8 Chinese characters + $v1 = ord($c); + if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character { - $lines[$lines_count++] = $line; - $line = $pre; - $line_count = strlen($line); - } - $line.=$str[$i]; - $line_count++; - if (ord($str[$i])>127) - $hz_ok = (!$hz_ok); - if (ord($str[$i])<=127) - $hz_ok = true; - } - $lines[$lines_count++] = $line; + $v2 = ($v1 & 0b01111000) << 1; + while ($v2 & 0b10000000) + { + $i++; + $v3 = $str[$i]; + $c .= $v3; + $v2 = ($v2 & 0b01111111 ) << 1; + } - $result = ""; - for($i=0;$i<$lines_count;$i++) - { - $result.=($lines[$i]."\r\n"); + $ret += 2; + } + else + { + $ret++; + } } - return $result; + + return $ret; } -function str_process($str,$pre="",$width=75) +function split_line(string $str, string $prefix = "", int $width = 76, int $lines_limit = PHP_INT_MAX, string $end_of_line = "\n") : string { - $result = ""; + if ($width <= 0) + { + return $str; + } - $lines = explode("\n",str_replace("\r\n","\n",$str)); + $result = ""; + $len = strlen($str); + $prefix_len = str_length($prefix); + + $lines_count = 0; - foreach($lines as $line) + $line = $prefix; + $line_len = $prefix_len; + for ($i = 0; $i < $len && $lines_count < $lines_limit; $i++) { - $result .= split_line($line,$pre,$width); + $c = $str[$i]; + + // Skip special characters + if ($c == "\r" || $c == "\7") + { + continue; + } + + if ($c == "\n") + { + if ($lines_count + 1 >= $lines_limit) + { + break; + } + + $result .= ($line . $end_of_line); + $lines_count++; + $line = $prefix; + $line_len = $prefix_len; + continue; + } + + // 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) + { + if ($lines_count + 1 >= $lines_limit) + { + break; + } + + $result .= ($line . $end_of_line); + $lines_count++; + $line = $prefix; + $line_len = $prefix_len; + } + $line_len += 2; + } + else + { + $line_len++; + } + + if ($line_len > $width) + { + if ($lines_count + 1 >= $lines_limit) + { + break; + } + + $result .= ($line . $end_of_line); + $lines_count++; + $line = $prefix; + $line_len = $prefix_len + 1; + } + + $line .= $c; } - + + if ($lines_count < $lines_limit) + { + $result .= $line; + } + return $result; } - ?>