/[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.12 by sysadm, Wed Mar 26 07:09:08 2025 UTC Revision 1.22 by sysadm, Thu Oct 30 11:19:14 2025 UTC
# Line 1  Line 1 
1  <?  <?php
2  function str_length($str)  function str_length(string $str) : int
3  {  {
4          $len = strlen($str);          $len = strlen($str);
5          $ret = 0;          $ret = 0;
# Line 10  function str_length($str) Line 10  function str_length($str)
10    
11                  // Process UTF-8 Chinese characters                  // Process UTF-8 Chinese characters
12                  $v1 = ord($c);                  $v1 = ord($c);
13                  if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character                  if ($v1 & 0x80) //head of multi-byte character
14                  {                  {
15                          $v2 = ($v1 & 0b01111000) << 1;                          $v2 = ($v1 & 0x70) << 1;
16                          while ($v2 & 0b10000000)                          while ($v2 & 0x80)
17                          {                          {
18                                  $i++;                                  $i++;
19                                  $v3 = $str[$i];                                  $c .= $str[$i];
20                                  $c .= $v3;                                  $v2 = ($v2 & 0x7f) << 1;
                                 $v2 = ($v2 & 0b01111111 ) << 1;  
21                          }                          }
22    
23                          $ret += 2;                          $ret += 2;
# Line 32  function str_length($str) Line 31  function str_length($str)
31          return $ret;          return $ret;
32  }  }
33    
34  function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX)  function split_line(string $str, string $prefix = "", int $width = PHP_INT_MAX, int $lines_limit = PHP_INT_MAX, string $end_of_line = "\n") : string
35  {  {
36          if ($width <= 0)          if ($width <= 0)
37                  return $str;          {
38                            $width = PHP_INT_MAX;
39            }
40    
41          $result = "";          $result = "";
42          $len = strlen($str);          $len = strlen($str);
43          $prefix_len = str_length($prefix);          $prefix_len = str_length($prefix);
# Line 54  function split_line($str, $prefix = "", Line 55  function split_line($str, $prefix = "",
55                  {                  {
56                          continue;                          continue;
57                  }                  }
58                                    
59                  if ($c == "\n")                  if ($c == "\n")
60                  {                  {
61                          $result .= ($line . "\n");                          if ($lines_count + 1 >= $lines_limit)
62                            {
63                                    break;
64                            }
65    
66                            $result .= ($line . $end_of_line);
67                          $lines_count++;                          $lines_count++;
68                          $line = $prefix;                          $line = $prefix;
69                          $line_len = $prefix_len;                          $line_len = $prefix_len;
# Line 66  function split_line($str, $prefix = "", Line 72  function split_line($str, $prefix = "",
72    
73                  // Process UTF-8 Chinese characters                  // Process UTF-8 Chinese characters
74                  $v1 = ord($c);                  $v1 = ord($c);
75                  if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character                  if ($v1 & 0x80) //head of multi-byte character
76                  {                  {
77                          $v2 = ($v1 & 0b01111000) << 1;                          $v2 = ($v1 & 0x70) << 1;
78                          while ($v2 & 0b10000000)                          while ($v2 & 0x80)
79                          {                          {
80                                  $i++;                                  $i++;
81                                  $v3 = $str[$i];                                  $c .= $str[$i];
82                                  $c .= $v3;                                  $v2 = ($v2 & 0x7f) << 1;
                                 $v2 = ($v2 & 0b01111111 ) << 1;  
83                          }                          }
84    
85                          // Each UTF-8 CJK character should use two character length for display                          // Each UTF-8 CJK character should use two character length for display
# Line 85  function split_line($str, $prefix = "", Line 90  function split_line($str, $prefix = "",
90                                          break;                                          break;
91                                  }                                  }
92    
93                                  $result .= ($line . "\n");                                  $result .= ($line . $end_of_line);
94                                  $lines_count++;                                  $lines_count++;
95                                  $line = $prefix;                                  $line = $prefix;
96                                  $line_len = $prefix_len;                                  $line_len = $prefix_len;
# Line 97  function split_line($str, $prefix = "", Line 102  function split_line($str, $prefix = "",
102                          $line_len++;                          $line_len++;
103                  }                  }
104    
105                  $line .= $c;                  if ($line_len > $width)
   
                 if ($line_len >= $width)  
106                  {                  {
107                          if ($lines_count + 1 >= $lines_limit)                          if ($lines_count + 1 >= $lines_limit)
108                          {                          {
109                                  break;                                  break;
110                          }                          }
111    
112                          $result .= ($line . "\n");                          $result .= ($line . $end_of_line);
113                          $lines_count++;                          $lines_count++;
114                          $line = $prefix;                          $line = $prefix;
115                          $line_len = $prefix_len;                          $line_len = $prefix_len + 1;
116                  }                  }
117    
118                    $line .= $c;
119          }          }
120    
121          if ($lines_count < $lines_limit)          if ($lines_count < $lines_limit)
# Line 120  function split_line($str, $prefix = "", Line 125  function split_line($str, $prefix = "",
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