/[LeafOK_CVS]/lbbs/utils/lib/str_process.inc.php
ViewVC logotype

Annotation of /lbbs/utils/lib/str_process.inc.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Fri Oct 31 10:46:19 2025 UTC (4 months, 2 weeks ago) by sysadm
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +39 -11 lines
Sync from leafok_bbs

1 sysadm 1.1 <?php
2 sysadm 1.4 function str_length(string $str, bool $skip_ctrl_seq = false) : int
3 sysadm 1.1 {
4     $len = strlen($str);
5     $ret = 0;
6    
7     for ($i = 0; $i < $len; $i++)
8     {
9     $c = $str[$i];
10    
11 sysadm 1.4 if ($c == "\r" || $c == "\7") // skip
12     {
13     continue;
14     }
15    
16     if ($skip_ctrl_seq && $c == "\033" && $i + 1 < $len && $str[$i + 1] == "[") // Skip control sequence
17     {
18     for ($i = $i + 2;
19     $i < $len && (ctype_digit($str[$i]) || $str[$i] == ';' || $str[$i] == '?');
20     $i++)
21     ;
22    
23     if ($i < $len && $str[$i] == 'm') // valid
24     {
25     // skip
26     }
27     else if ($i < $len && ctype_alpha($str[$i]))
28     {
29     // unsupported ANSI CSI command
30     }
31     else
32     {
33     $i--;
34     }
35    
36     continue;
37     }
38    
39 sysadm 1.3 // Process UTF-8 Chinese characters
40 sysadm 1.1 $v1 = ord($c);
41 sysadm 1.4 if ($v1 & 0x80) //head of multi-byte character
42 sysadm 1.1 {
43 sysadm 1.4 $v2 = ($v1 & 0x70) << 1;
44     while ($v2 & 0x80)
45 sysadm 1.3 {
46     $i++;
47     $c .= $str[$i];
48 sysadm 1.4 $v2 = ($v2 & 0x7f) << 1;
49 sysadm 1.3 }
50 sysadm 1.1
51     $ret += 2;
52     }
53     else
54     {
55     $ret++;
56     }
57     }
58    
59     return $ret;
60     }
61    
62 sysadm 1.4 function split_line(string $str, string $prefix = "", int $width = PHP_INT_MAX, int $lines_limit = PHP_INT_MAX, string $end_of_line = "\n") : string
63 sysadm 1.1 {
64     if ($width <= 0)
65     {
66 sysadm 1.4 $width = PHP_INT_MAX;
67 sysadm 1.1 }
68    
69     $result = "";
70     $len = strlen($str);
71     $prefix_len = str_length($prefix);
72    
73     $lines_count = 0;
74    
75     $line = $prefix;
76     $line_len = $prefix_len;
77     for ($i = 0; $i < $len && $lines_count < $lines_limit; $i++)
78     {
79     $c = $str[$i];
80    
81     // Skip special characters
82     if ($c == "\r" || $c == "\7")
83     {
84     continue;
85     }
86    
87     if ($c == "\n")
88     {
89     if ($lines_count + 1 >= $lines_limit)
90     {
91     break;
92     }
93    
94     $result .= ($line . $end_of_line);
95     $lines_count++;
96     $line = $prefix;
97     $line_len = $prefix_len;
98     continue;
99     }
100    
101 sysadm 1.3 // Process UTF-8 Chinese characters
102 sysadm 1.1 $v1 = ord($c);
103 sysadm 1.4 if ($v1 & 0x80) //head of multi-byte character
104 sysadm 1.1 {
105 sysadm 1.4 $v2 = ($v1 & 0x70) << 1;
106     while ($v2 & 0x80)
107 sysadm 1.3 {
108     $i++;
109     $c .= $str[$i];
110 sysadm 1.4 $v2 = ($v2 & 0x7f) << 1;
111 sysadm 1.3 }
112 sysadm 1.1
113 sysadm 1.3 // Each UTF-8 CJK character should use two character length for display
114 sysadm 1.1 if ($line_len + 2 > $width)
115     {
116     if ($lines_count + 1 >= $lines_limit)
117     {
118     break;
119     }
120    
121     $result .= ($line . $end_of_line);
122     $lines_count++;
123     $line = $prefix;
124     $line_len = $prefix_len;
125     }
126     $line_len += 2;
127     }
128     else
129     {
130     $line_len++;
131     }
132    
133     if ($line_len > $width)
134     {
135     if ($lines_count + 1 >= $lines_limit)
136     {
137     break;
138     }
139    
140     $result .= ($line . $end_of_line);
141     $lines_count++;
142     $line = $prefix;
143     $line_len = $prefix_len + 1;
144     }
145    
146     $line .= $c;
147     }
148    
149     if ($lines_count < $lines_limit)
150     {
151     $result .= $line;
152     }
153    
154     return $result;
155     }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1