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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.3 by sysadm, Fri Jun 25 08:58:04 2004 UTC Revision 1.15 by sysadm, Mon Apr 7 06:26:44 2025 UTC
# Line 1  Line 1 
1  <?  <?
2    function str_length(string $str) : string
 function split_line($str,$pre="",$width=75)  
3  {  {
         $lines = array();  
         $lines_count = 0;  
4          $len = strlen($str);          $len = strlen($str);
5            $ret = 0;
6    
7          $hz_ok = true;          for ($i = 0; $i < $len; $i++)
8          $en_ok = true;          {
9                            $c = $str[$i];
10          if ($width<=0)  
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    function split_line(string $str, string $prefix = "", int $width = 76, int $lines_limit = PHP_INT_MAX, string $end_of_line = "\n") : string
36    {
37            if ($width <= 0)
38            {
39                  return $str;                  return $str;
40            }
41                    
42          $line = $pre;          $result = "";
43          $line_count = strlen($line);          $len = strlen($str);
44          for($i=0;$i<$len;$i++)          $prefix_len = str_length($prefix);
45    
46            $lines_count = 0;
47    
48            $line = $prefix;
49            $line_len = $prefix_len;
50            for ($i = 0; $i < $len && $lines_count < $lines_limit; $i++)
51          {          {
52                  $line.=$str[$i];                  $c = $str[$i];
53                  $line_count++;  
54                  if (ord($str[$i])>127)                  // Skip special characters
55                    if ($c == "\r" || $c == "\7")
56                  {                  {
57                          $hz_ok = (!$hz_ok);                          continue;
                         $en_ok = true;  
58                  }                  }
59                  if (ord($str[$i])<=127)                                  
60                    if ($c == "\n")
61                  {                  {
62                          $hz_ok = true;                          if ($lines_count + 1 >= $lines_limit)
                         if ($str[$i] == " ")  
63                          {                          {
64                                  $en_ok = true;                                  break;
65                          }                          }
66                          else  
67                            $result .= ($line . $end_of_line);
68                            $lines_count++;
69                            $line = $prefix;
70                            $line_len = $prefix_len;
71                            continue;
72                    }
73    
74                    // Process UTF-8 Chinese characters
75                    $v1 = ord($c);
76                    if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character
77                    {
78                            $v2 = ($v1 & 0b01111000) << 1;
79                            while ($v2 & 0b10000000)
80                            {
81                                    $i++;
82                                    $v3 = $str[$i];
83                                    $c .= $v3;
84                                    $v2 = ($v2 & 0b01111111 ) << 1;
85                            }
86    
87                            // Each UTF-8 CJK character should use two character length for display
88                            if ($line_len + 2 > $width)
89                          {                          {
90                                  $en_ok = false;                                  if ($lines_count + 1 >= $lines_limit)
91                                    {
92                                            break;
93                                    }
94    
95                                    $result .= ($line . $end_of_line);
96                                    $lines_count++;
97                                    $line = $prefix;
98                                    $line_len = $prefix_len;
99                          }                          }
100                            $line_len += 2;
101                  }                  }
102                  if ($line_count >= $width && $hz_ok && $en_ok)                  else
103                  {                  {
104                          $lines[$lines_count++] = $line;                          $line_len++;
                         $line = $pre;  
                         $line_count = strlen($line);  
105                  }                  }
         }  
         $lines[$lines_count++] = $line;  
106    
107          $result = "";                  if ($line_len > $width)
108          for($i=0;$i<$lines_count;$i++)                  {
109          {                          if ($lines_count + 1 >= $lines_limit)
110                  $result.=($lines[$i]."\r\n");                          {
111          }                                  break;
112          return $result;                          }
 }  
113    
114  function str_process($str,$pre="",$width=75)                          $result .= ($line . $end_of_line);
115  {                          $lines_count++;
116          $result = "";                          $line = $prefix;
117                                    $line_len = $prefix_len + 1;
118          $lines = explode("\n",str_replace("\r\n","\n",$str));                  }
119    
120                    $line .= $c;
121            }
122    
123          foreach($lines as $line)          if ($lines_count < $lines_limit)
124          {          {
125                  $result .= split_line($line,$pre,$width);                  $result .= $line;
126          }          }
127            
128          return $result;          return $result;
129  }  }
   
130  ?>  ?>


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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