/[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.6 by sysadm, Sat Mar 12 08:01:19 2005 UTC Revision 1.12 by sysadm, Wed Mar 26 07:09:08 2025 UTC
# Line 1  Line 1 
1  <?  <?
2    function str_length($str)
 function split_line($str,$prefix="",$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 = $prefix;  
         $line_count = strlen($line);  
         for($i=0;$i<$len;$i++)  
8          {          {
9                  $line.=$str[$i];                  $c = $str[$i];
10                  $line_count++;  
11                  if (ord($str[$i])>127)                  // Process UTF-8 Chinese characters
12                  {                  $v1 = ord($c);
13                          $hz_ok = (!$hz_ok);                  if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character
                 }  
                 if (ord($str[$i])<=127)  
14                  {                  {
15                          $hz_ok = true;                          $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                  if ($line_count >= $width && $hz_ok)                  else
27                  {                  {
28                          $lines[$lines_count++] = $line;                          $ret++;
                         $line = $prefix;  
                         $line_count = strlen($line);  
29                  }                  }
30          }          }
         $lines[$lines_count++] = $line;  
31    
32          $result = "";          return $ret;
         for($i=0;$i<$lines_count;$i++)  
         {  
                 $result.=($lines[$i]."\r\n");  
         }  
         return $result;  
33  }  }
34    
35  function str_process($str,$prefix="",$width=75)  function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX)
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[0],$prefix,$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                            $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                                    if ($lines_count + 1 >= $lines_limit)
84                                    {
85                                            break;
86                                    }
87    
88                                    $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    
102                    if ($line_len >= $width)
103                    {
104                            if ($lines_count + 1 >= $lines_limit)
105                            {
106                                    break;
107                            }
108    
109                            $result .= ($line . "\n");
110                            $lines_count++;
111                            $line = $prefix;
112                            $line_len = $prefix_len;
113                    }
114          }          }
           
         return $result;  
 }  
115    
116  function str_b_lines($str,$max_line=1)          if ($lines_count < $lines_limit)
 {  
         $result = "";  
         $l = 0;  
         $lines = explode("\n",str_replace("\r\n","\n",$str));  
         foreach($lines as $line)  
117          {          {
118                  $result .= ($line[0]."\r\n");                  $result .= $line;
                 $l += (strlen($line[0])/256 + 1);  
                 if ($l >= $max_line)  
                         break;  
119          }          }
120    
121          return $result;          return $result;
122  }  }
   
123  ?>  ?>


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

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