/[LeafOK_CVS]/fenglin/lib/str_process.inc.php
ViewVC logotype

Annotation of /fenglin/lib/str_process.inc.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations)
Wed Mar 26 07:09:08 2025 UTC (11 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.11: +44 -1 lines
Add str_length() to calculate the length of string containing UTF-8 based CJK characters

1 sysadm 1.1 <?
2 sysadm 1.12 function str_length($str)
3     {
4     $len = strlen($str);
5     $ret = 0;
6    
7     for ($i = 0; $i < $len; $i++)
8     {
9     $c = $str[$i];
10    
11     // Process UTF-8 Chinese characters
12     $v1 = ord($c);
13     if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character
14     {
15     $v2 = ($v1 & 0b01111000) << 1;
16     while ($v2 & 0b10000000)
17     {
18     $i++;
19     $v3 = $str[$i];
20     $c .= $v3;
21     $v2 = ($v2 & 0b01111111 ) << 1;
22     }
23    
24     $ret += 2;
25     }
26     else
27     {
28     $ret++;
29     }
30     }
31    
32     return $ret;
33     }
34    
35 sysadm 1.10 function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX)
36 sysadm 1.1 {
37 sysadm 1.10 if ($width <= 0)
38 sysadm 1.1 return $str;
39    
40 sysadm 1.9 $result = "";
41 sysadm 1.10 $len = strlen($str);
42 sysadm 1.12 $prefix_len = str_length($prefix);
43 sysadm 1.10
44     $lines_count = 0;
45 sysadm 1.9
46 sysadm 1.4 $line = $prefix;
47 sysadm 1.9 $line_len = $prefix_len;
48 sysadm 1.11 for ($i = 0; $i < $len && $lines_count < $lines_limit; $i++)
49 sysadm 1.1 {
50 sysadm 1.10 $c = $str[$i];
51 sysadm 1.9
52 sysadm 1.10 // Skip special characters
53     if ($c == "\r" || $c == "\7")
54     {
55     continue;
56     }
57    
58     if ($c == "\n")
59     {
60     $result .= ($line . "\n");
61     $lines_count++;
62     $line = $prefix;
63     $line_len = $prefix_len;
64     continue;
65     }
66    
67     // Process UTF-8 Chinese characters
68     $v1 = ord($c);
69     if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character
70     {
71     $v2 = ($v1 & 0b01111000) << 1;
72     while ($v2 & 0b10000000)
73     {
74     $i++;
75     $v3 = $str[$i];
76     $c .= $v3;
77     $v2 = ($v2 & 0b01111111 ) << 1;
78     }
79    
80     // Each UTF-8 CJK character should use two character length for display
81     if ($line_len + 2 > $width)
82     {
83 sysadm 1.12 if ($lines_count + 1 >= $lines_limit)
84     {
85     break;
86     }
87    
88 sysadm 1.10 $result .= ($line . "\n");
89     $lines_count++;
90     $line = $prefix;
91     $line_len = $prefix_len;
92     }
93     $line_len += 2;
94     }
95     else
96     {
97     $line_len++;
98     }
99    
100     $line .= $c;
101 sysadm 1.9
102     if ($line_len >= $width)
103 sysadm 1.3 {
104 sysadm 1.12 if ($lines_count + 1 >= $lines_limit)
105     {
106     break;
107     }
108    
109 sysadm 1.9 $result .= ($line . "\n");
110 sysadm 1.10 $lines_count++;
111 sysadm 1.4 $line = $prefix;
112 sysadm 1.9 $line_len = $prefix_len;
113 sysadm 1.3 }
114 sysadm 1.1 }
115 sysadm 1.11
116     if ($lines_count < $lines_limit)
117     {
118     $result .= $line;
119     }
120 sysadm 1.1
121     return $result;
122     }
123     ?>

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