/[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.2 - (hide annotations)
Mon May 19 01:47:28 2025 UTC (9 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.1: +1 -1 lines
Fix bug

1 sysadm 1.1 <?php
2 sysadm 1.2 function str_length(string $str) : 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     // Process GBK Chinese characters
12     $v1 = ord($c);
13     if ($v1 > 127 && $v1 <= 255) // GBK chinese character
14     {
15     $i++;
16     $c .= $str[$i];
17    
18     $ret += 2;
19     }
20     else
21     {
22     $ret++;
23     }
24     }
25    
26     return $ret;
27     }
28    
29     function split_line(string $str, string $prefix = "", int $width = 76, int $lines_limit = PHP_INT_MAX, string $end_of_line = "\n") : string
30     {
31     if ($width <= 0)
32     {
33     return $str;
34     }
35    
36     $result = "";
37     $len = strlen($str);
38     $prefix_len = str_length($prefix);
39    
40     $lines_count = 0;
41    
42     $line = $prefix;
43     $line_len = $prefix_len;
44     for ($i = 0; $i < $len && $lines_count < $lines_limit; $i++)
45     {
46     $c = $str[$i];
47    
48     // Skip special characters
49     if ($c == "\r" || $c == "\7")
50     {
51     continue;
52     }
53    
54     if ($c == "\n")
55     {
56     if ($lines_count + 1 >= $lines_limit)
57     {
58     break;
59     }
60    
61     $result .= ($line . $end_of_line);
62     $lines_count++;
63     $line = $prefix;
64     $line_len = $prefix_len;
65     continue;
66     }
67    
68     // Process GBK Chinese characters
69     $v1 = ord($c);
70     if ($v1 > 127 && $v1 <= 255) // GBK chinese character
71     {
72     $i++;
73     $c .= $str[$i];
74    
75     // Each GBK CJK character should use two character length for display
76     if ($line_len + 2 > $width)
77     {
78     if ($lines_count + 1 >= $lines_limit)
79     {
80     break;
81     }
82    
83     $result .= ($line . $end_of_line);
84     $lines_count++;
85     $line = $prefix;
86     $line_len = $prefix_len;
87     }
88     $line_len += 2;
89     }
90     else
91     {
92     $line_len++;
93     }
94    
95     if ($line_len > $width)
96     {
97     if ($lines_count + 1 >= $lines_limit)
98     {
99     break;
100     }
101    
102     $result .= ($line . $end_of_line);
103     $lines_count++;
104     $line = $prefix;
105     $line_len = $prefix_len + 1;
106     }
107    
108     $line .= $c;
109     }
110    
111     if ($lines_count < $lines_limit)
112     {
113     $result .= $line;
114     }
115    
116     return $result;
117     }

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