PHPMailer::encodeQ PHP Method

encodeQ() public method

Encode a string using Q encoding.
public encodeQ ( string $str, string $position = 'text' ) : string
$str string the text to encode
$position string Where the text is going to be used, see the RFC for what that means
return string
    public function encodeQ($str, $position = 'text')
    {
        // There should not be any EOL in the string
        $pattern = '';
        $encoded = str_replace(array("\r", "\n"), '', $str);
        switch (strtolower($position)) {
            case 'phrase':
                // RFC 2047 section 5.3
                $pattern = '^A-Za-z0-9!*+\\/ -';
                break;
                /** @noinspection PhpMissingBreakStatementInspection */
            /** @noinspection PhpMissingBreakStatementInspection */
            case 'comment':
                // RFC 2047 section 5.2
                $pattern = '\\(\\)"';
                // intentional fall-through
                // for this reason we build the $pattern without including delimiters and []
            // intentional fall-through
            // for this reason we build the $pattern without including delimiters and []
            case 'text':
            default:
                // RFC 2047 section 5.1
                // Replace every high ascii, control, =, ? and _ characters
                $pattern = '\\000-\\011\\013\\014\\016-\\037\\075\\077\\137\\177-\\377' . $pattern;
                break;
        }
        $matches = array();
        if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) {
            // If the string contains an '=', make sure it's the first thing we replace
            // so as to avoid double-encoding
            $eqkey = array_search('=', $matches[0]);
            if (false !== $eqkey) {
                unset($matches[0][$eqkey]);
                array_unshift($matches[0], '=');
            }
            foreach (array_unique($matches[0]) as $char) {
                $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded);
            }
        }
        // Replace every spaces to _ (more readable than =20)
        return str_replace(' ', '_', $encoded);
    }

Usage Example

 /**
  * Encoding and charset tests.
  */
 public function testEncodings()
 {
     $this->Mail->CharSet = 'iso-8859-1';
     $this->assertEquals('=A1Hola!_Se=F1or!', $this->Mail->encodeQ("�Hola! Se�or!", 'text'), 'Q Encoding (text) failed');
     $this->assertEquals('=A1Hola!_Se=F1or!', $this->Mail->encodeQ("�Hola! Se�or!", 'comment'), 'Q Encoding (comment) failed');
     $this->assertEquals('=A1Hola!_Se=F1or!', $this->Mail->encodeQ("�Hola! Se�or!", 'phrase'), 'Q Encoding (phrase) failed');
     $this->Mail->CharSet = 'UTF-8';
     $this->assertEquals('=C2=A1Hola!_Se=C3=B1or!', $this->Mail->encodeQ("¡Hola! Señor!", 'text'), 'Q Encoding (text) failed');
     //Strings containing '=' are a special case
     $this->assertEquals('Nov=C3=A1=3D', $this->Mail->encodeQ("Nová=", 'text'), 'Q Encoding (text) failed 2');
 }
All Usage Examples Of PHPMailer::encodeQ