fixLineEnding() public static method

Fixes and unifies line endings in a string.
public static fixLineEnding ( string $string, string $lineEnd = " " ) : string
$string string String to fix
$lineEnd string Desired line ending
return string
Beispiel #1
0
 /**
  * Adds all attachments to the email.
  *
  * @return string
  */
 private function attachAll() : string
 {
     $mime = [];
     foreach ($this->email->attachments as $attachment) {
         $encoding = !empty($attachment->encoding) ? $attachment->encoding : Encoding::BASE64;
         $name = $this->changeCharset($this->clearHeaderValue($attachment->name));
         $mime[] = '--' . $this->boundary[1] . self::LINE_END;
         $mime[] = 'Content-Type: ' . $this->clearHeaderValue($attachment->mimeType) . ';' . self::LINE_END;
         $mime[] = ' name="' . $this->encodeHeader($name) . '"' . self::LINE_END;
         $mime[] = 'Content-Transfer-Encoding: ' . $encoding . self::LINE_END;
         if ($attachment->isInline()) {
             $mime[] = 'Content-ID: <' . $this->clearHeaderValue($attachment->cid) . '>' . self::LINE_END;
         }
         $mime[] = 'Content-Disposition: ' . $attachment->disposition . ';' . self::LINE_END;
         $mime[] = ' filename="' . $this->encodeHeader($name) . '"' . self::LINE_END . self::LINE_END;
         // Just fix line endings in case of encoded attachments, encode otherwise
         $mime[] = !empty($attachment->encoding) ? \Jyxo\StringUtil::fixLineEnding($attachment->content, self::LINE_END) : $this->encodeString($attachment->content, $encoding);
         $mime[] = self::LINE_END . self::LINE_END;
     }
     $mime[] = '--' . $this->boundary[1] . '--' . self::LINE_END;
     return implode('', $mime);
 }
Beispiel #2
0
 /**
  * Encodes a string using the quoted-printable encoding.
  *
  * @param string $string Input string
  * @param integer $lineLength Line length
  * @param string $lineEnd Line ending
  * @return string
  */
 private static function encodeQuotedPrintable(string $string, int $lineLength, string $lineEnd) : string
 {
     $encoded = \Jyxo\StringUtil::fixLineEnding(trim($string), $lineEnd);
     // Replaces all high ASCII characters, control codes and '='
     $encoded = preg_replace_callback('~([\\000-\\010\\013\\014\\016-\\037\\075\\177-\\377])~', function ($matches) {
         return '=' . sprintf('%02X', ord($matches[1]));
     }, $encoded);
     // Replaces tabs and spaces if on line ends
     $encoded = preg_replace_callback('~([\\011\\040])' . $lineEnd . '~', function ($matches) use($lineEnd) {
         return '=' . sprintf('%02X', ord($matches[1])) . $lineEnd;
     }, $encoded);
     $output = '';
     $lines = explode($lineEnd, $encoded);
     // Release from memory
     unset($encoded);
     foreach ($lines as $line) {
         // Line length is less than maximum
         if (strlen($line) <= $lineLength) {
             $output .= $line . $lineEnd;
             continue;
         }
         do {
             $partLength = strlen($line);
             if ($partLength > $lineLength) {
                 $partLength = $lineLength;
             }
             // Cannot break a line in the middle of a character
             $pos = strrpos(substr($line, 0, $partLength), '=');
             if (false !== $pos && $pos >= $partLength - 2) {
                 $partLength = $pos;
             }
             // If the last char is a break, move one character backwards
             if ($partLength > 0 && ' ' == $line[$partLength - 1]) {
                 $partLength--;
             }
             // Saves string parts, trims the string and continues
             $output .= substr($line, 0, $partLength);
             $line = substr($line, $partLength);
             // We are in the middle of a line
             if (!empty($line)) {
                 $output .= '=';
             }
             $output .= $lineEnd;
         } while (!empty($line));
     }
     return $output;
 }