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


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

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