/[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.11 by sysadm, Mon Mar 24 05:46:49 2025 UTC Revision 1.14 by sysadm, Fri Apr 4 03:01:37 2025 UTC
# Line 1  Line 1 
1  <?  <?
2  function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX)  function str_length(string $str) : string
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(string $str, string $prefix = "", int $width = 76, int $lines_limit = PHP_INT_MAX, string $end_of_line = "\n") : string
36  {  {
37          if ($width <= 0)          if ($width <= 0)
38            {
39                  return $str;                  return $str;
40            }
41                    
42          $result = "";          $result = "";
43          $len = strlen($str);          $len = strlen($str);
44          $prefix_len = strlen($prefix);          $prefix_len = str_length($prefix);
45    
46          $lines_count = 0;          $lines_count = 0;
47    
# Line 24  function split_line($str, $prefix = "", Line 59  function split_line($str, $prefix = "",
59                                                                    
60                  if ($c == "\n")                  if ($c == "\n")
61                  {                  {
62                          $result .= ($line . "\n");                          if ($lines_count + 1 >= $lines_limit)
63                            {
64                                    break;
65                            }
66    
67                            $result .= ($line . $end_of_line);
68                          $lines_count++;                          $lines_count++;
69                          $line = $prefix;                          $line = $prefix;
70                          $line_len = $prefix_len;                          $line_len = $prefix_len;
# Line 47  function split_line($str, $prefix = "", Line 87  function split_line($str, $prefix = "",
87                          // Each UTF-8 CJK character should use two character length for display                          // Each UTF-8 CJK character should use two character length for display
88                          if ($line_len + 2 > $width)                          if ($line_len + 2 > $width)
89                          {                          {
90                                  $result .= ($line . "\n");                                  if ($lines_count + 1 >= $lines_limit)
91                                    {
92                                            break;
93                                    }
94    
95                                    $result .= ($line . $end_of_line);
96                                  $lines_count++;                                  $lines_count++;
97                                  $line = $prefix;                                  $line = $prefix;
98                                  $line_len = $prefix_len;                                  $line_len = $prefix_len;
# Line 63  function split_line($str, $prefix = "", Line 108  function split_line($str, $prefix = "",
108    
109                  if ($line_len >= $width)                  if ($line_len >= $width)
110                  {                  {
111                          $result .= ($line . "\n");                          if ($lines_count + 1 >= $lines_limit)
112                            {
113                                    break;
114                            }
115    
116                            $result .= ($line . $end_of_line);
117                          $lines_count++;                          $lines_count++;
118                          $line = $prefix;                          $line = $prefix;
119                          $line_len = $prefix_len;                          $line_len = $prefix_len;


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

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