]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/include/rcube_shared.inc
Imported Upstream version 0.2.2
[roundcube.git] / program / include / rcube_shared.inc
index 370b4bc33e3dae74d48d0ae8575872da869b265c..ccf30d2f61ca0e27a6642944c7d8b018e9088735 100644 (file)
@@ -15,7 +15,7 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  +-----------------------------------------------------------------------+
 
- $Id: rcube_shared.inc 2313 2009-02-27 10:18:18Z thomasb $
+ $Id: rcube_shared.inc 2483 2009-05-15 10:22:29Z thomasb $
 
 */
 
@@ -405,6 +405,54 @@ function rc_strrpos($haystack, $needle, $offset=0)
     return strrpos($haystack, $needle, $offset);
 }
 
+/**
+ * Wrapper function for wordwrap
+ */
+function rc_wordwrap($string, $width=75, $break="\n", $cut=false)
+{
+  if (!function_exists('mb_substr') || !function_exists('mb_strlen'))
+    return wordwrap($string, $width, $break, $cut);
+    
+  $para = explode($break, $string);
+  $string = '';
+  while (count($para)) {
+    $list = explode(' ', array_shift($para));
+    $len = 0;
+    while (count($list)) {
+      $line = array_shift($list);
+      $l = mb_strlen($line);
+      $newlen = $len + $l + ($len ? 1 : 0);
+
+      if ($newlen <= $width) {
+        $string .= ($len ? ' ' : '').$line;
+        $len += ($len ? 1 : 0) + $l;
+      } else {
+       if ($l > $width) {
+         if ($cut) {
+           $start = 0;
+           while ($l) {
+             $str = mb_substr($line, $start, $width);
+             $strlen = mb_strlen($str);
+             $string .= ($len ? $break : '').$str;
+             $start += $strlen;
+             $l -= $strlen;
+             $len = $strlen;
+           }
+         } else {
+            $string .= ($len ? $break : '').$line;
+           if (count($list)) $string .= $break;
+           $len = 0;
+         }
+       } else {
+          $string .= $break.$line;
+         $len = $l;
+        }
+      }
+    }
+    if (count($para)) $string .= $break;
+  }
+  return $string;
+}
 
 /**
  * Read a specific HTTP request header