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


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

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