- function sqWordWrap($passed, $wrap) {
- $passed = str_replace("<", "<", $passed);
- $passed = str_replace(">", ">", $passed);
-
- preg_match("/^(\s|>)+/", $passed, $regs);
- $beginning_spaces = $regs[0];
-
- $words = explode(" ", $passed);
- $i = -1;
- $line_len = strlen($words[0])+1;
- $line = "";
- if (count($words) > 1) {
- while ($i++ < count($words)) {
- while ($line_len < $wrap) {
- $line = "$line$words[$i] ";
- $i++;
- $line_len = $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 <BR> 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<BR>";
- }
- */
- if (strlen($line) > $wrap)
- $line = "$line\n$words[$i]";
- else
- $line = "$line$words[$i]";
- $line_len = strlen($words[$i]);
- }
+ // Has a problem with special HTML characters, so call this before
+ // you do character translation.
+ // Specifically, ' comes up as 5 characters instead of 1.
+ // This should not add newlines to the end of lines.
+ function sqWordWrap(&$line, $wrap) {
+ preg_match("/^([\s>]*)([^\s>].*)?$/", $line, $regs);
+ $beginning_spaces = $regs[1];
+ $words = explode(" ", $regs[2]);
+
+ $i = 0;
+ $line = $beginning_spaces;
+
+ while ($i < count($words)) {
+ // Force one word to be on a line (minimum)
+ $line .= $words[$i];
+ $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2;
+ if (isset($words[$i + 1]))
+ $line_len += strlen($words[$i + 1]);
+ $i ++;
+
+ // Add more words (as long as they fit)
+ while ($line_len < $wrap && $i < count($words)) {
+ $line .= ' ' . $words[$i];
+ $i++;
+ if (isset($words[$i]))
+ $line_len += strlen($words[$i]) + 1;
+ else
+ $line_len += 1;
+ }
+
+ // Skip spaces if they are the first thing on a continued line
+ while (!isset($words[$i]) && $i < count($words)) {
+ $i ++;