Updated sqWordWrap() function a lot
authorfidian <fidian@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Tue, 10 Oct 2000 17:24:56 +0000 (17:24 +0000)
committerfidian <fidian@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Tue, 10 Oct 2000 17:24:56 +0000 (17:24 +0000)
git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@787 7612ce4b-ef26-0410-bec9-ea0150e637f0

functions/strings.php

index 02e5fc3ad741096fab8c091ba539bc2f10d387e7..94e7d0512d5f41ab94c8376565cbab4532334237 100644 (file)
    }
 
    // Wraps text at $wrap characters
+   // Has a problem with special HTML characters, so call this before
+   // you do character translation.
+   // Specifically, &#039 comes up as 5 characters instead of 1.
    function sqWordWrap(&$line, $wrap) {
-      $line = str_replace("&lt;", "<", $line);
-      $line = str_replace("&gt;", ">", $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 <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]);
+            if ($i < count($words)) {  // If there's more to do, worry about it
+               $line .= "\n$beginning_spaces";
             }
          }
       } else {
          $line = $words[0];
       }
-
-      $line = str_replace(">", "&gt;", $line);
-      $line = str_replace("<", "&lt;", $line);
    }
 
    /** Returns an array of email addresses **/
       $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(' ', '&nbsp;', $line);
          $line = nl2br($line);