| 1 |
<? |
<?php |
| 2 |
|
function str_length(string $str) : int |
|
function split_line($str,$pre="",$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 = $pre; |
|
|
$line_count = strlen($line); |
|
|
for($i=0;$i<$len;$i++) |
|
| 8 |
{ |
{ |
| 9 |
if ($line_count >= $width && $hz_ok) |
$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 |
$lines[$lines_count++] = $line; |
$v2 = ($v1 & 0b01111000) << 1; |
| 16 |
$line = $pre; |
while ($v2 & 0b10000000) |
| 17 |
$line_count = strlen($line); |
{ |
| 18 |
} |
$i++; |
| 19 |
$line.=$str[$i]; |
$v3 = $str[$i]; |
| 20 |
$line_count++; |
$c .= $v3; |
| 21 |
if (ord($str[$i])>127) |
$v2 = ($v2 & 0b01111111 ) << 1; |
| 22 |
$hz_ok = (!$hz_ok); |
} |
|
if (ord($str[$i])<=127) |
|
|
$hz_ok = true; |
|
|
} |
|
|
$lines[$lines_count++] = $line; |
|
| 23 |
|
|
| 24 |
$result = ""; |
$ret += 2; |
| 25 |
for($i=0;$i<$lines_count;$i++) |
} |
| 26 |
{ |
else |
| 27 |
$result.=($lines[$i]."\r\n"); |
{ |
| 28 |
|
$ret++; |
| 29 |
|
} |
| 30 |
} |
} |
| 31 |
return $result; |
|
| 32 |
|
return $ret; |
| 33 |
} |
} |
| 34 |
|
|
| 35 |
function str_process($str,$pre="",$width=75) |
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) |
| 38 |
|
{ |
| 39 |
|
return $str; |
| 40 |
|
} |
| 41 |
|
|
| 42 |
$result = ""; |
$result = ""; |
| 43 |
|
$len = strlen($str); |
| 44 |
$lines = explode("\n",str_replace("\r\n","\n",$str)); |
$prefix_len = str_length($prefix); |
| 45 |
|
|
| 46 |
foreach($lines as $line) |
$lines_count = 0; |
| 47 |
|
|
| 48 |
|
$line = $prefix; |
| 49 |
|
$line_len = $prefix_len; |
| 50 |
|
for ($i = 0; $i < $len && $lines_count < $lines_limit; $i++) |
| 51 |
{ |
{ |
| 52 |
$result .= split_line($line,$pre,$width); |
$c = $str[$i]; |
| 53 |
|
|
| 54 |
|
// Skip special characters |
| 55 |
|
if ($c == "\r" || $c == "\7") |
| 56 |
|
{ |
| 57 |
|
continue; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
if ($c == "\n") |
| 61 |
|
{ |
| 62 |
|
if ($lines_count + 1 >= $lines_limit) |
| 63 |
|
{ |
| 64 |
|
break; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
$result .= ($line . $end_of_line); |
| 68 |
|
$lines_count++; |
| 69 |
|
$line = $prefix; |
| 70 |
|
$line_len = $prefix_len; |
| 71 |
|
continue; |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
// Process UTF-8 Chinese characters |
| 75 |
|
$v1 = ord($c); |
| 76 |
|
if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character |
| 77 |
|
{ |
| 78 |
|
$v2 = ($v1 & 0b01111000) << 1; |
| 79 |
|
while ($v2 & 0b10000000) |
| 80 |
|
{ |
| 81 |
|
$i++; |
| 82 |
|
$v3 = $str[$i]; |
| 83 |
|
$c .= $v3; |
| 84 |
|
$v2 = ($v2 & 0b01111111 ) << 1; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
// Each UTF-8 CJK character should use two character length for display |
| 88 |
|
if ($line_len + 2 > $width) |
| 89 |
|
{ |
| 90 |
|
if ($lines_count + 1 >= $lines_limit) |
| 91 |
|
{ |
| 92 |
|
break; |
| 93 |
|
} |
| 94 |
|
|
| 95 |
|
$result .= ($line . $end_of_line); |
| 96 |
|
$lines_count++; |
| 97 |
|
$line = $prefix; |
| 98 |
|
$line_len = $prefix_len; |
| 99 |
|
} |
| 100 |
|
$line_len += 2; |
| 101 |
|
} |
| 102 |
|
else |
| 103 |
|
{ |
| 104 |
|
$line_len++; |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
if ($line_len > $width) |
| 108 |
|
{ |
| 109 |
|
if ($lines_count + 1 >= $lines_limit) |
| 110 |
|
{ |
| 111 |
|
break; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
$result .= ($line . $end_of_line); |
| 115 |
|
$lines_count++; |
| 116 |
|
$line = $prefix; |
| 117 |
|
$line_len = $prefix_len + 1; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
$line .= $c; |
| 121 |
} |
} |
| 122 |
|
|
| 123 |
|
if ($lines_count < $lines_limit) |
| 124 |
|
{ |
| 125 |
|
$result .= $line; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
return $result; |
return $result; |
| 129 |
} |
} |
|
|
|
|
?> |
|