note 82000 modified in function.quoted-printable-decode by vrana

From: Date: Wed, 26 Mar 2008 14:52:25 +0000
Subject: note 82000 modified in function.quoted-printable-decode by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137597@lists.php.net to get a copy of this message
<?php $text = <<<EOF This function enables you to convert text to a quoted-printable string as well as to create encoded-words used in email headers (see http://www.faqs.org/rfcs/rfc2047.html). No line of returned text will be longer than specified. Encoded-words will not contain a newline character. Special characters are removed. EOF; define('QP_LINE_LENGTH', 75); define('QP_LINE_SEPARATOR', "\r\n"); function quoted_printable_encode($string, $encodedWord = false) { if(!preg_match('//u', $string)) { throw new Exception('Input string is not valid UTF-8'); } static $wordStart = '=?UTF-8?Q?'; static $wordEnd = '?='; static $endl = QP_LINE_SEPARATOR; $lineLength = $encodedWord ? QP_LINE_LENGTH - strlen($wordStart) - strlen($wordEnd) : QP_LINE_LENGTH; $string = $encodedWord ? preg_replace('~[\r\n]+~', ' ', $string) // we need encoded word to be single line : preg_replace('~\r\n?~', "\n", $string); // normalize line endings $string = preg_replace('~[\x00-\x08\x0B-\x1F]+~', '', $string); // remove control characters $output = $encodedWord ? $wordStart : ''; $charsLeft = $lineLength; $chr = isset($string{0}) ? $string{0} : null; $ord = ord($chr); for ($i = 0; isset($chr); $i++) { $nextChr = isset($string{$i + 1}) ? $string{$i + 1} : null; $nextOrd = ord($nextChr); if ( $ord > 127 or // high byte value $ord === 95 or // underscore "_" $ord === 63 && $encodedWord or // "?" in encoded word $ord === 61 or // equal sign "=" // space or tab in encoded word or at line end $ord === 32 || $ord === 9 and $encodedWord || !isset($nextOrd) || $nextOrd === 10 ) { $chr = sprintf('=%02X', $ord); } if ($ord === 10) { // line feed $output .= $endl; $charsLeft = $lineLength; } elseif ( strlen($chr) < $charsLeft or strlen($chr) === $charsLeft and $nextOrd === 10 || $encodedWord ) { // add character $output .= $chr; $charsLeft-=strlen($chr); } elseif (isset($nextOrd)) { // another line needed $output .= $encodedWord ? $wordEnd . $endl . "\t" . $wordStart . $chr : '=' . $endl . $chr; $charsLeft = $lineLength - strlen($chr); } $chr = $nextChr; $ord = $nextOrd; } return $output . ($encodedWord ? $wordEnd : ''); } echo quoted_printable_encode($text/*, true*/); --was-- <?php $text = <<<EOF This function enables you to convert text to a quoted-printable string as well as to create encoded-words used in email headers (see http://www.faqs.org/rfcs/rfc2047.html). No line of returned text will be longer than specified. Encoded-words will not contain a newline character. Special characters are removed. EOF; define('QP_LINE_LENGTH', 75); define('QP_LINE_SEPARATOR', "\r\n"); function quoted_printable_encode($string, $encodedWord = false) { if(!preg_match('//u', $string)) { throw new Exception('Input string is not valid UTF-8'); } static $wordStart = '=?UTF-8?Q?'; static $wordEnd = '?='; static $endl = QP_LINE_SEPARATOR; $lineLength = $encodedWord ? QP_LINE_LENGTH - strlen($wordStart) - strlen($wordEnd) : QP_LINE_LENGTH; $string = $encodedWord ? preg_replace('~[\r\n]+~', ' ', $string) // we need encoded word to be single line : preg_replace('~\r\n?~', "\n", $string); // normalize line endings $string = preg_replace('~[\x00-\x08\x0B-\x1F]+~', '', $string); // remove control characters $output = $encodedWord ? $wordStart : ''; $charsLeft = $lineLength; $chr = isset($string{0}) ? $string{0} : null; $ord = ord($chr); for ($i = 0; isset($chr); $i++) { $nextChr = isset($string{$i + 1}) ? $string{$i + 1} : null; $nextOrd = ord($nextChr); if ( $ord > 127 or // high byte value $ord === 63 && $encodedWord or // "?" in encoded word $ord === 61 or // equal sign "=" $ord === 32 || $ord === 9 and // space or tab in encoded word or at line end $encodedWord || !isset($nextOrd) || $nextOrd === 10 ) { $chr = sprintf('=%02X', $ord); } if ($ord === 10) { // line feed $output .= $endl; $charsLeft = $lineLength; } elseif ( strlen($chr) < $charsLeft or strlen($chr) === $charsLeft and $nextOrd === 10 || $encodedWord ) { // add character $output .= $chr; $charsLeft-=strlen($chr); } elseif (isset($nextOrd)) { // another line needed $output .= $encodedWord ? $wordEnd . $endl . ' ' . $wordStart . $chr : '=' . $endl . $chr; $charsLeft = $lineLength - strlen($chr); } $chr = $nextChr; $ord = $nextOrd; } return $output . ($encodedWord ? $wordEnd : ''); } echo quoted_printable_encode($text/*, true*/); http://php.net/manual/en/function.quoted-printable-decode.php

« previous php.notes (#137597) next »