| 1 |
<? |
<? |
| 2 |
|
function split_line($str, $prefix = "", $width = 76, $lines_limit = PHP_INT_MAX) |
|
function split_line($str,$prefix="",$width=76) |
|
| 3 |
{ |
{ |
| 4 |
if ($width<=0) |
if ($width <= 0) |
| 5 |
return $str; |
return $str; |
| 6 |
|
|
| 7 |
$result = ""; |
$result = ""; |
| 8 |
$len = mb_strlen($str,'UTF-8'); |
$len = strlen($str); |
| 9 |
$prefix_len = mb_strlen($prefix,'UTF-8'); |
$prefix_len = strlen($prefix); |
| 10 |
|
|
| 11 |
|
$lines_count = 0; |
| 12 |
|
|
| 13 |
$line = $prefix; |
$line = $prefix; |
| 14 |
$line_len = $prefix_len; |
$line_len = $prefix_len; |
| 15 |
for($i=0;$i<$len;$i++) |
for ($i = 0; $i < $len && $lines_count < $lines_limit; $i++) |
| 16 |
{ |
{ |
| 17 |
$c = mb_substr($str,$i,1,'UTF-8'); |
$c = $str[$i]; |
|
$line.=$c; |
|
| 18 |
|
|
| 19 |
// Each UTF-8 CJK character should use two character length for display |
// Skip special characters |
| 20 |
$line_len += (strlen($c) <= 2 ? 1 : 2); |
if ($c == "\r" || $c == "\7") |
| 21 |
|
{ |
| 22 |
if ($line_len >= $width) |
continue; |
| 23 |
|
} |
| 24 |
|
|
| 25 |
|
if ($c == "\n") |
| 26 |
{ |
{ |
| 27 |
$result .= ($line . "\n"); |
$result .= ($line . "\n"); |
| 28 |
|
$lines_count++; |
| 29 |
$line = $prefix; |
$line = $prefix; |
| 30 |
$line_len = $prefix_len; |
$line_len = $prefix_len; |
| 31 |
|
continue; |
| 32 |
} |
} |
|
} |
|
|
$result .= ($line . "\n"); |
|
| 33 |
|
|
| 34 |
return $result; |
// Process UTF-8 Chinese characters |
| 35 |
} |
$v1 = ord($c); |
| 36 |
|
if (($v1 & 0b10000000) == 0b10000000) //head of multi-byte character |
| 37 |
|
{ |
| 38 |
|
$v2 = ($v1 & 0b01111000) << 1; |
| 39 |
|
while ($v2 & 0b10000000) |
| 40 |
|
{ |
| 41 |
|
$i++; |
| 42 |
|
$v3 = $str[$i]; |
| 43 |
|
$c .= $v3; |
| 44 |
|
$v2 = ($v2 & 0b01111111 ) << 1; |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
// Each UTF-8 CJK character should use two character length for display |
| 48 |
|
if ($line_len + 2 > $width) |
| 49 |
|
{ |
| 50 |
|
$result .= ($line . "\n"); |
| 51 |
|
$lines_count++; |
| 52 |
|
$line = $prefix; |
| 53 |
|
$line_len = $prefix_len; |
| 54 |
|
} |
| 55 |
|
$line_len += 2; |
| 56 |
|
} |
| 57 |
|
else |
| 58 |
|
{ |
| 59 |
|
$line_len++; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
function str_process($str,$prefix="",$width=76) |
$line .= $c; |
|
{ |
|
|
$result = ""; |
|
|
|
|
|
$lines = explode("\n",str_replace("\r\n","\n",$str)); |
|
| 63 |
|
|
| 64 |
foreach($lines as $tmp => $line) |
if ($line_len >= $width) |
| 65 |
{ |
{ |
| 66 |
$result .= split_line($line,$prefix,$width); |
$result .= ($line . "\n"); |
| 67 |
|
$lines_count++; |
| 68 |
|
$line = $prefix; |
| 69 |
|
$line_len = $prefix_len; |
| 70 |
|
} |
| 71 |
} |
} |
|
|
|
|
return $result; |
|
|
} |
|
| 72 |
|
|
| 73 |
function str_b_lines($str,$max_line=1) |
if ($lines_count < $lines_limit) |
|
{ |
|
|
$result = ""; |
|
|
$l = 0; |
|
|
$lines = explode("\n",str_replace("\r\n","\n",$str)); |
|
|
foreach($lines as $tmp => $line) |
|
| 74 |
{ |
{ |
| 75 |
$result .= ($line."\n"); |
$result .= $line; |
|
$l += (mb_strlen($line,'UTF-8')/128 + 1); |
|
|
if ($l >= $max_line) |
|
|
break; |
|
| 76 |
} |
} |
| 77 |
|
|
| 78 |
return $result; |
return $result; |
| 79 |
} |
} |
|
|
|
| 80 |
?> |
?> |