note 41039 deleted from function.wordwrap by aidan
| From: | aidan@php.net | Date: | Mon, 19 Jul 2004 23:25:14 +0000 |
| Subject: | note 41039 deleted from function.wordwrap by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73644@lists.php.net to get a copy of this message | ||
Note Submitter: cfranklin@nomadcf.com
----
I tryed every word warp on the web and this site but none of them really worked nicely, all of them
left be fragmented lines or wraped a single word the the next line and then did a line returned.
Anyways... This wordwrap can sense the line endings of the text sent to it, join lines for a cleaner
look or use the default type like all the others, indent lines or mark then how ever you want to
show there a carry over from the line above. AND it doesn't break, HTML tages !!!
function word_wrap($org_message, $cols="85", $returns="AUTO",
$spacer="", $joiner=' ')
{
/*
$org_message = full text you want to wrap into smaller lines
$cols = number of char before we need to wrap
$cut = what to use as the line breaker
$returns = what type of line returns is being used ?
(AUTO will try to figure out the line ending)
$spacer = addon to the cut, a \t will indent every
line with a tab
$joiner = This is what will join too lines. When
needed.
*/
if ($returns == 'AUTO') {
$MS = substr_count($org_message,"\r\n");
$Linux = substr_count($org_message,"\n");
$Mac = substr_count($org_message,"\r");
if ($MS >= $Linux && $MS >= $Mac ) {
$returns = "\r\n";
} elseif ($Linux > $Mac) {
$returns = "\n";
} elseif ($Linux < $Mac) {
$returns = "\r";
}
}
$message = explode($returns,$org_message);
$msg_lines = count($message);
$removed = array('');
$open = 0;
for ($a=0;$a<$msg_lines;$a++) {
$line = $message[$a];
$lenght = strlen($line);
$new_line = '';
$num = 0;
$changed = 0;
if ($lenght > $cols ) {
for ($b=0; $b<=$lenght;$b++){
$addon = '';
switch ($line[$b]) {
case '<':
$open++;
break;
case '>':
if ($open > 0) {
$open--;
}
break;
case ' ':
if ($open == 0 && $num >=$cols ) {
$addon = $returns;
$chnaged = 1;
$num = 0;
} elseif($open == 0 && $b == strrpos($line,' ') && $chnaged == 0) {
$addon = $returns;
$num = 0;
}
break;
}
$num++;
if ($addon) {
$new_line .= $addon.$spacer;
} else {
$new_line .= $line[$b];
}
}
} else {
$new_line = $line;
}
if ($message[$a] != $new_line) {
$message[$a] = $new_line.$joiner.$message[$a+1];
if ($message[$a+1] == '') {
$message[$a] .= $returns;
}
$message[$a+1] = '';
$removed[] = $a+1;
}
}
for ($a=0;$a<count($message);$a++) {
if (!array_search($a,$removed)) {
$new_message .= $message[$a].$returns;
}
}
return $new_message;
}