note 44263 added to function.wordwrap
| From: | youngjin at thompsonkim dot net | Date: | Mon, 19 Jul 2004 22:47:37 +0000 |
| Subject: | note 44263 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-73641@lists.php.net to get a copy of this message | ||
This is a modification of the word_wrap function submitted by cfranklin at nomadcf dot com on
27-Mar-2004 11:24 (listed below) with only a minor modifications to fix a bug that kept creeping up
when I used it for my wordwrapping (the original script tried accessing a character in an character
array where there was no character, i.e. "for ( $b = 0; $b < $length; $b++ )" used to
be "for ( $b = 0; $b <= $length; $b++ )".
function word_wrap($org_message, $cols="80", $returns="AUTO",
$spacer="", $joiner=' ')
{
/*
$org_message = full text you want to wrap into smaller lines
$cols = number of char before we need to wrap
$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];
$length = strlen($line);
$new_line = '';
$num = 0;
$changed = 0;
if ( $length > $cols ) {
for ( $b = 0; $b < $length; $b++ ){
$addon = '';
switch ( $line[$b] ) {
case "<":
$open++;
break;
case ">":
if ($open > 0) {
$open--;
}
break;
case " ":
if ($open == 0 && $num >=$cols ) {
$addon = $returns;
$changed = 1;
$num = 0;
} elseif($open == 0 && $b == strrpos($line,' ') && $changed == 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;
}
}
$new_message = '';
for ( $a=0 ; $a < count($message); $a++ ) {
if (!array_search($a,$removed)) {
$new_message .= $message[$a].$returns;
}
}
return $new_message;
}#end function word_wrap($org_message, $cols="80", $returns="AUTO",
$spacer="", $joiner=' ')
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44263
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44263&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44263&report=yes
Search -- http://master.php.net/manage/user-notes.php