/[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.10 - (hide annotations)
Tue Mar 18 07:10:59 2025 UTC (11 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.9: +53 -41 lines
Refact

1 sysadm 1.1 <?
2 sysadm 1.10 function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX)
3 sysadm 1.1 {
4 sysadm 1.10 if ($width <= 0)
5 sysadm 1.1 return $str;
6    
7 sysadm 1.9 $result = "";
8 sysadm 1.10 $len = strlen($str);
9     $prefix_len = mb_strlen($prefix, 'UTF-8');
10    
11     $lines_count = 0;
12 sysadm 1.9
13 sysadm 1.4 $line = $prefix;
14 sysadm 1.9 $line_len = $prefix_len;
15 sysadm 1.10 for($i = 0; $i < $len && $lines_count < $lines_limit; $i++)
16 sysadm 1.1 {
17 sysadm 1.10 $c = $str[$i];
18 sysadm 1.9
19 sysadm 1.10 // Skip special characters
20     if ($c == "\r" || $c == "\7")
21     {
22     continue;
23     }
24    
25     if ($c == "\n")
26     {
27     $result .= ($line . "\n");
28     $lines_count++;
29     $line = $prefix;
30     $line_len = $prefix_len;
31     continue;
32     }
33    
34     // Process UTF-8 Chinese characters
35     $v1 = ord($c);
36     if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character
37     {
38     $v2 = ($v1 & 0b01111000) << 1;
39     while ($v2 & 0b10000000)
40     {
41     $i++;
42     $v3 = $str[$i];
43     $c .= $v3;
44     $v2 = ($v2 & 0b01111111 ) << 1;
45     }
46    
47     // Each UTF-8 CJK character should use two character length for display
48     if ($line_len + 2 > $width)
49     {
50     $result .= ($line . "\n");
51     $lines_count++;
52     $line = $prefix;
53     $line_len = $prefix_len;
54     }
55     $line_len += 2;
56     }
57     else
58     {
59     $line_len++;
60     }
61    
62     $line .= $c;
63 sysadm 1.9
64     if ($line_len >= $width)
65 sysadm 1.3 {
66 sysadm 1.9 $result .= ($line . "\n");
67 sysadm 1.10 $lines_count++;
68 sysadm 1.4 $line = $prefix;
69 sysadm 1.9 $line_len = $prefix_len;
70 sysadm 1.3 }
71 sysadm 1.1 }
72 sysadm 1.9 $result .= ($line . "\n");
73 sysadm 1.1
74     return $result;
75     }
76     ?>

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