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


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

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