Guest User

t3lib_utility_Mail::breakLinesForEmail

a guest
Jun 25th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1.     static public function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76) {
  2.         $lines = array();
  3.         $substrStart = 0;
  4.         while (strlen($str) > $substrStart) {
  5.             $substr = substr($str, $substrStart, $lineWidth);
  6.             // has line exceeded (reached) the maximum width?
  7.             if (strlen($substr) == $lineWidth) {
  8.                 // find last space-char
  9.                 $spacePos = strrpos(rtrim($substr), ' ');
  10.                 // space-char found?
  11.                 if ($spacePos !== FALSE) {
  12.                     // take everything up to last space-char
  13.                     $theLine = substr($substr, 0, $spacePos);
  14.                     $substrStart++;
  15.                 } else {
  16.                     // search for space-char in remaining text
  17.                     // makes this line longer than $lineWidth!
  18.                     $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
  19.                     $theLine = $substr . $afterParts[0];
  20.                 }
  21.                 if (!strlen($theLine)) {
  22.                     // prevent endless loop because of empty line
  23.                     break;
  24.                 }
  25.             } else {
  26.                 $theLine = $substr;
  27.             }
  28.             $lines[] = trim($theLine);
  29.             $substrStart += strlen($theLine);
  30.             if (trim(substr($str, $substrStart, $lineWidth)) === '') {
  31.                 // no more text
  32.                 break;
  33.             }
  34.         }
  35.         return implode($newlineChar, $lines);
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment