/[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.10 by sysadm, Tue Mar 18 07:10:59 2025 UTC Revision 1.13 by sysadm, Fri Mar 28 10:31:26 2025 UTC
# Line 1  Line 1 
1  <?  <?
2  function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX)  function str_length($str)
3    {
4            $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) == 0b10000000) //head of multi-byte character
14                    {
15                            $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                    else
27                    {
28                            $ret++;
29                    }
30            }
31    
32            return $ret;
33    }
34    
35    function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX, $end_of_line = "\n")
36  {  {
37          if ($width <= 0)          if ($width <= 0)
38                  return $str;                  return $str;
39                    
40          $result = "";          $result = "";
41          $len = strlen($str);          $len = strlen($str);
42          $prefix_len = mb_strlen($prefix, 'UTF-8');          $prefix_len = str_length($prefix);
43    
44          $lines_count = 0;          $lines_count = 0;
45    
46          $line = $prefix;          $line = $prefix;
47          $line_len = $prefix_len;          $line_len = $prefix_len;
48          for($i = 0; $i < $len && $lines_count < $lines_limit; $i++)          for ($i = 0; $i < $len && $lines_count < $lines_limit; $i++)
49          {          {
50                  $c = $str[$i];                  $c = $str[$i];
51    
# Line 24  function split_line($str, $prefix = "", Line 57  function split_line($str, $prefix = "",
57                                                                    
58                  if ($c == "\n")                  if ($c == "\n")
59                  {                  {
60                          $result .= ($line . "\n");                          if ($lines_count + 1 >= $lines_limit)
61                            {
62                                    break;
63                            }
64    
65                            $result .= ($line . $end_of_line);
66                          $lines_count++;                          $lines_count++;
67                          $line = $prefix;                          $line = $prefix;
68                          $line_len = $prefix_len;                          $line_len = $prefix_len;
# Line 47  function split_line($str, $prefix = "", Line 85  function split_line($str, $prefix = "",
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
86                          if ($line_len + 2 > $width)                          if ($line_len + 2 > $width)
87                          {                          {
88                                  $result .= ($line . "\n");                                  if ($lines_count + 1 >= $lines_limit)
89                                    {
90                                            break;
91                                    }
92    
93                                    $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 63  function split_line($str, $prefix = "", Line 106  function split_line($str, $prefix = "",
106    
107                  if ($line_len >= $width)                  if ($line_len >= $width)
108                  {                  {
109                          $result .= ($line . "\n");                          if ($lines_count + 1 >= $lines_limit)
110                            {
111                                    break;
112                            }
113    
114                            $result .= ($line . $end_of_line);
115                          $lines_count++;                          $lines_count++;
116                          $line = $prefix;                          $line = $prefix;
117                          $line_len = $prefix_len;                          $line_len = $prefix_len;
118                  }                  }
119          }          }
120          $result .= ($line . "\n");  
121            if ($lines_count < $lines_limit)
122            {
123                    $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