note 82000 added to function.quoted-printable-decode

From: Date: Sun, 23 Mar 2008 16:22:46 +0000
Subject: note 82000 added to function.quoted-printable-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-137362@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 === 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*/); ---- Server IP: 212.24.129.51 Probable Submitter: 213.29.65.108 ---- Manual Page -- http://www.php.net/manual/en/function.quoted-printable-decode.php Edit -- https://master.php.net/note/edit/82000 Del: integrated -- https://master.php.net/note/delete/82000/integrated Del: useless -- https://master.php.net/note/delete/82000/useless Del: bad code -- https://master.php.net/note/delete/82000/bad+code Del: spam -- https://master.php.net/note/delete/82000/spam Del: non-english -- https://master.php.net/note/delete/82000/non-english Del: in docs -- https://master.php.net/note/delete/82000/in+docs Del: other reasons-- https://master.php.net/note/delete/82000 Reject -- https://master.php.net/note/reject/82000 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137362) next »