From a95681a74ae3014f317852eed218b982be0a626d Mon Sep 17 00:00:00 2001 From: fidian Date: Tue, 10 Oct 2000 17:24:56 +0000 Subject: [PATCH] Updated sqWordWrap() function a lot git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@787 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- functions/strings.php | 62 ++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/functions/strings.php b/functions/strings.php index 02e5fc3a..94e7d051 100644 --- a/functions/strings.php +++ b/functions/strings.php @@ -45,56 +45,36 @@ } // Wraps text at $wrap characters + // Has a problem with special HTML characters, so call this before + // you do character translation. + // Specifically, ' comes up as 5 characters instead of 1. function sqWordWrap(&$line, $wrap) { - $line = str_replace("<", "<", $line); - $line = str_replace(">", ">", $line); - preg_match("/^(\s|>)+/", $line, $regs); $beginning_spaces = $regs[0]; - + $words = explode(" ", $line); - $i = -1; - $line_len = strlen($words[0])+1; - $line = ""; - if (count($words) > 1) { - while ($i++ < count($words)) { + + $i = 0; + $line = $beginning_spaces; + if (count($words) > 1) { + while ($i < count($words)) { + // Force one word to be on a line (minimum) + $line .= $words[$i] . ' '; + $line_len = strlen($beginning_spaces) + strlen($words[$i]) + + strlen($words[$i + 1]) + 2; + $i ++; while ($line_len < $wrap && $i < count($words)) { - $line = "$line$words[$i] "; + $line .= $words[$i] . ' '; $i++; - $line_len = $line_len + strlen($words[$i]) + 1; + $line_len += strlen($words[$i]) + 1; } - $line_len = strlen($words[$i])+1; - if ($line_len <= $wrap) { - if (strlen($beginning_spaces) +2 >= $wrap) - $beginning_spaces = ""; - if ($i < count($words)) { // don't
the last line - $line = "$line\n$beginning_spaces"; - } - $line = "$line$words[$i] "; - $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 1; - } else { - /* - $endline = $words[$i]; - while ($line_len >= $wrap) { - $bigline = substr($endline, 0, $wrap); - $endline = substr($endline, $wrap, strlen($endline)); - $line_len = strlen($endline); - $line = "$line$bigline
"; - } - */ - if (strlen($line) > $wrap) - $line = "$line\n$words[$i]"; - else - $line = "$line$words[$i]"; - $line_len = strlen($words[$i]); + if ($i < count($words)) { // If there's more to do, worry about it + $line .= "\n$beginning_spaces"; } } } else { $line = $words[0]; } - - $line = str_replace(">", ">", $line); - $line = str_replace("<", "<", $line); } /** Returns an array of email addresses **/ @@ -134,13 +114,11 @@ $body_ary = explode("\n", $body); for ($i=0; $i < count($body_ary); $i++) { $line = $body_ary[$i]; - $line = charset_decode($charset, $line); - $line = str_replace("\t", ' ', $line); - chop($line); - if (strlen($line) - 2 >= $wrap_at) { sqWordWrap($line, $wrap_at); } + $line = charset_decode($charset, $line); + $line = str_replace("\t", ' ', $line); $line = str_replace(' ', ' ', $line); $line = nl2br($line); -- 2.25.1