| 1 |
<? |
<? |
| 2 |
|
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) |
function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX) |
| 36 |
{ |
{ |
| 37 |
if ($width <= 0) |
if ($width <= 0) |
| 39 |
|
|
| 40 |
$result = ""; |
$result = ""; |
| 41 |
$len = strlen($str); |
$len = strlen($str); |
| 42 |
$prefix_len = strlen($prefix); |
$prefix_len = str_length($prefix); |
| 43 |
|
|
| 44 |
$lines_count = 0; |
$lines_count = 0; |
| 45 |
|
|
| 80 |
// Each UTF-8 CJK character should use two character length for display |
// Each UTF-8 CJK character should use two character length for display |
| 81 |
if ($line_len + 2 > $width) |
if ($line_len + 2 > $width) |
| 82 |
{ |
{ |
| 83 |
|
if ($lines_count + 1 >= $lines_limit) |
| 84 |
|
{ |
| 85 |
|
break; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
$result .= ($line . "\n"); |
$result .= ($line . "\n"); |
| 89 |
$lines_count++; |
$lines_count++; |
| 90 |
$line = $prefix; |
$line = $prefix; |
| 101 |
|
|
| 102 |
if ($line_len >= $width) |
if ($line_len >= $width) |
| 103 |
{ |
{ |
| 104 |
|
if ($lines_count + 1 >= $lines_limit) |
| 105 |
|
{ |
| 106 |
|
break; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
$result .= ($line . "\n"); |
$result .= ($line . "\n"); |
| 110 |
$lines_count++; |
$lines_count++; |
| 111 |
$line = $prefix; |
$line = $prefix; |