Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static public function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76) {
- $lines = array();
- $substrStart = 0;
- while (strlen($str) > $substrStart) {
- $substr = substr($str, $substrStart, $lineWidth);
- // has line exceeded (reached) the maximum width?
- if (strlen($substr) == $lineWidth) {
- // find last space-char
- $spacePos = strrpos(rtrim($substr), ' ');
- // space-char found?
- if ($spacePos !== FALSE) {
- // take everything up to last space-char
- $theLine = substr($substr, 0, $spacePos);
- $substrStart++;
- } else {
- // search for space-char in remaining text
- // makes this line longer than $lineWidth!
- $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
- $theLine = $substr . $afterParts[0];
- }
- if (!strlen($theLine)) {
- // prevent endless loop because of empty line
- break;
- }
- } else {
- $theLine = $substr;
- }
- $lines[] = trim($theLine);
- $substrStart += strlen($theLine);
- if (trim(substr($str, $substrStart, $lineWidth)) === '') {
- // no more text
- break;
- }
- }
- return implode($newlineChar, $lines);
- }
Advertisement
Add Comment
Please, Sign In to add comment