| 1 |
<? |
<? |
| 2 |
|
|
| 3 |
function split_line($str,$pre="",$width=75) |
function split_line($str,$prefix="",$width=76) |
| 4 |
{ |
{ |
|
$lines = array(); |
|
|
$lines_count = 0; |
|
|
$len = strlen($str); |
|
|
|
|
|
$hz_ok = true; |
|
|
|
|
| 5 |
if ($width<=0) |
if ($width<=0) |
| 6 |
return $str; |
return $str; |
| 7 |
|
|
| 8 |
$line = $pre; |
$result = ""; |
| 9 |
$line_count = strlen($line); |
$len = mb_strlen($str,'UTF-8'); |
| 10 |
|
$prefix_len = mb_strlen($prefix,'UTF-8'); |
| 11 |
|
|
| 12 |
|
$line = $prefix; |
| 13 |
|
$line_len = $prefix_len; |
| 14 |
for($i=0;$i<$len;$i++) |
for($i=0;$i<$len;$i++) |
| 15 |
{ |
{ |
| 16 |
if ($line_count >= $width && $hz_ok) |
$c = mb_substr($str,$i,1,'UTF-8'); |
| 17 |
|
$line.=$c; |
| 18 |
|
|
| 19 |
|
// Each UTF-8 CJK character should use two character length for display |
| 20 |
|
$line_len += (strlen($c) <= 2 ? 1 : 2); |
| 21 |
|
|
| 22 |
|
if ($line_len >= $width) |
| 23 |
{ |
{ |
| 24 |
$lines[$lines_count++] = $line; |
$result .= ($line . "\n"); |
| 25 |
$line = $pre; |
$line = $prefix; |
| 26 |
$line_count = strlen($line); |
$line_len = $prefix_len; |
| 27 |
} |
} |
|
$line.=$str[$i]; |
|
|
$line_count++; |
|
|
if (ord($str[$i])>127) |
|
|
$hz_ok = (!$hz_ok); |
|
|
if (ord($str[$i])<=127) |
|
|
$hz_ok = true; |
|
| 28 |
} |
} |
| 29 |
$lines[$lines_count++] = $line; |
$result .= ($line . "\n"); |
| 30 |
|
|
|
$result = ""; |
|
|
for($i=0;$i<$lines_count;$i++) |
|
|
{ |
|
|
$result.=($lines[$i]."\r\n"); |
|
|
} |
|
| 31 |
return $result; |
return $result; |
| 32 |
} |
} |
| 33 |
|
|
| 34 |
function str_process($str,$pre="",$width=75) |
function str_process($str,$prefix="",$width=76) |
| 35 |
{ |
{ |
| 36 |
$result = ""; |
$result = ""; |
| 37 |
|
|
| 38 |
$lines = explode("\n",str_replace("\r\n","\n",$str)); |
$lines = explode("\n",str_replace("\r\n","\n",$str)); |
| 39 |
|
|
| 40 |
foreach($lines as $line) |
foreach($lines as $tmp => $line) |
| 41 |
{ |
{ |
| 42 |
$result .= split_line($line,$pre,$width); |
$result .= split_line($line,$prefix,$width); |
| 43 |
} |
} |
| 44 |
|
|
| 45 |
return $result; |
return $result; |
| 46 |
} |
} |
| 47 |
|
|
| 48 |
|
function str_b_lines($str,$max_line=1) |
| 49 |
|
{ |
| 50 |
|
$result = ""; |
| 51 |
|
$l = 0; |
| 52 |
|
$lines = explode("\n",str_replace("\r\n","\n",$str)); |
| 53 |
|
foreach($lines as $tmp => $line) |
| 54 |
|
{ |
| 55 |
|
$result .= ($line."\n"); |
| 56 |
|
$l += (mb_strlen($line,'UTF-8')/128 + 1); |
| 57 |
|
if ($l >= $max_line) |
| 58 |
|
break; |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
return $result; |
| 62 |
|
} |
| 63 |
|
|
| 64 |
?> |
?> |