Swift_Encoder_QpEncoder::encodeString PHP Method

encodeString() public method

QP encoded strings have a maximum line length of 76 characters. If the first line needs to be shorter, indicate the difference with $firstLineOffset.
public encodeString ( string $string, $firstLineOffset, $maxLineLength ) : string
$string string to encode
return string
    public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
    {
        if ($maxLineLength > 76 || $maxLineLength <= 0) {
            $maxLineLength = 76;
        }
        $thisLineLength = $maxLineLength - $firstLineOffset;
        $lines = array();
        $lNo = 0;
        $lines[$lNo] = '';
        $currentLine =& $lines[$lNo++];
        $size = $lineLen = 0;
        $this->_charStream->flushContents();
        $this->_charStream->importString($string);
        // Fetching more than 4 chars at one is slower, as is fetching fewer bytes
        // Conveniently 4 chars is the UTF-8 safe number since UTF-8 has up to 6
        // bytes per char and (6 * 4 * 3 = 72 chars per line) * =NN is 3 bytes
        while (false !== ($bytes = $this->_nextSequence())) {
            // If we're filtering the input
            if (isset($this->_filter)) {
                // If we can't filter because we need more bytes
                while ($this->_filter->shouldBuffer($bytes)) {
                    // Then collect bytes into the buffer
                    if (false === ($moreBytes = $this->_nextSequence(1))) {
                        break;
                    }
                    foreach ($moreBytes as $b) {
                        $bytes[] = $b;
                    }
                }
                // And filter them
                $bytes = $this->_filter->filter($bytes);
            }
            $enc = $this->_encodeByteSequence($bytes, $size);
            $i = strpos($enc, '=0D=0A');
            $newLineLength = $lineLen + ($i === false ? $size : $i);
            if ($currentLine && $newLineLength >= $thisLineLength) {
                $lines[$lNo] = '';
                $currentLine =& $lines[$lNo++];
                $thisLineLength = $maxLineLength;
                $lineLen = 0;
            }
            $currentLine .= $enc;
            if ($i === false) {
                $lineLen += $size;
            } else {
                // 6 is the length of '=0D=0A'.
                $lineLen = $size - strrpos($enc, '=0D=0A') - 6;
            }
        }
        return $this->_standardize(implode("=\r\n", $lines));
    }

Usage Example

 public function testEncodingAndDecodingSamples()
 {
     $sampleFp = opendir($this->_samplesDir);
     while (false !== ($encodingDir = readdir($sampleFp))) {
         if (substr($encodingDir, 0, 1) == '.') {
             continue;
         }
         $encoding = $encodingDir;
         $charStream = new Swift_CharacterStream_ArrayCharacterStream($this->_factory, $encoding);
         $encoder = new Swift_Encoder_QpEncoder($charStream);
         $sampleDir = $this->_samplesDir . '/' . $encodingDir;
         if (is_dir($sampleDir)) {
             $fileFp = opendir($sampleDir);
             while (false !== ($sampleFile = readdir($fileFp))) {
                 if (substr($sampleFile, 0, 1) == '.') {
                     continue;
                 }
                 $text = file_get_contents($sampleDir . '/' . $sampleFile);
                 $encodedText = $encoder->encodeString($text);
                 foreach (explode("\r\n", $encodedText) as $line) {
                     $this->assertLessThanOrEqual(76, strlen($line));
                 }
                 $this->assertEquals(quoted_printable_decode($encodedText), $text, '%s: Encoded string should decode back to original string for sample ' . $sampleDir . '/' . $sampleFile);
             }
             closedir($fileFp);
         }
     }
     closedir($sampleFp);
 }
All Usage Examples Of Swift_Encoder_QpEncoder::encodeString