wppaste
WordPress

PHPMailer::encodeHeader( string $str, string $position = 'text' ): string

Source
wp-includes/PHPMailer/PHPMailer.php:3723
Encode a header value (not including its label) optimally.

Description

Picks shortest of Q, B, or none. Result includes folding if needed.
See RFC822 definitions for phrase, comment and text positions, and RFC2047 for inline encodings.

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$strstring
The header value to encode
$positionstringoptional
What context the string will be used inDefault: 'text'

Return value

string

Performance profile

How much work a call to PHPMailer::encodeHeader() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–92

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 164.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • regexregular expression over the whole inputpreg_match_all()called directly

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost PHPMailer::encodeHeader() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$position !== "comment"15strtolower(), ::normalizeBreaks()
!$str26–33strtolower(), strtolower(), addcslashes()
always40–58strtolower(), strtolower(), preg_match_all(), ->has8bitChars()
always45–62strtolower(), strtolower(), preg_match_all(), preg_match_all(), ->has8bitChars()
->hasMultiBytes()63–79strtolower(), strtolower(), preg_match_all(), ->has8bitChars(), ->hasMultiBytes(), ->base64EncodeWrapMB(), ::normalizeBreaks()
->hasMultiBytes()68–83strtolower(), strtolower(), preg_match_all(), preg_match_all(), ->has8bitChars(), ->hasMultiBytes(), ->base64EncodeWrapMB(), ::normalizeBreaks()
always70–88strtolower(), strtolower(), preg_match_all(), ->has8bitChars(), ->encodeQ(), ->wrapText(), ::normalizeBreaks()
!->hasMultiBytes()70–86strtolower(), strtolower(), preg_match_all(), ->has8bitChars(), ->hasMultiBytes(), base64_encode(), chunk_split(), ::normalizeBreaks()
always75–92strtolower(), strtolower(), preg_match_all(), preg_match_all(), ->has8bitChars(), ->encodeQ(), ->wrapText(), ::normalizeBreaks()
!->hasMultiBytes()75–90strtolower(), strtolower(), preg_match_all(), preg_match_all(), ->has8bitChars(), ->hasMultiBytes(), base64_encode(), chunk_split(), ::normalizeBreaks()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev16415–9217
8.516415–9217
8.416415–921723 fewer instructions than PHP 8.3
8.318717–10217
8.218717–10217
8.118717–102171 more instruction than PHP 7.4
7.418617–10217

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

  • static::normalizeBreaks()
  • \PHPMailer\PHPMailer\PHPMailer::has8bitChars()
  • \PHPMailer\PHPMailer\PHPMailer::hasMultiBytes()
  • \PHPMailer\PHPMailer\PHPMailer::base64EncodeWrapMB()
  • \PHPMailer\PHPMailer\PHPMailer::encodeQ()
  • \PHPMailer\PHPMailer\PHPMailer::wrapText()

Source code

    public function encodeHeader($str, $position = 'text')    {        $position = strtolower($position);        if ($this->UseSMTPUTF8 && !("comment" === $position)) {            return trim(static::normalizeBreaks($str));        }         $matchcount = 0;        switch (strtolower($position)) {            case 'phrase':                if (!preg_match('/[\200-\377]/', $str)) {                    //Can't use addslashes as we don't know the value of magic_quotes_sybase                    $encoded = addcslashes($str, "\0..\37\177\\\"");                    if (($str === $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {                        return $encoded;                    }                     return "\"$encoded\"";                }                $matchcount = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);                break;            /* @noinspection PhpMissingBreakStatementInspection */            case 'comment':                $matchcount = preg_match_all('/[()"]/', $str, $matches);            //fallthrough            case 'text':            default:                $matchcount += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);                break;        }         if ($this->has8bitChars($str)) {            $charset = $this->CharSet;        } else {            $charset = static::CHARSET_ASCII;        }         //Q/B encoding adds 8 chars and the charset ("` =?<charset>?[QB]?<content>?=`").        $overhead = 8 + strlen($charset);         if ('mail' === $this->Mailer) {            $maxlen = static::MAIL_MAX_LINE_LENGTH - $overhead;        } else {            $maxlen = static::MAX_LINE_LENGTH - $overhead;        }         //Select the encoding that produces the shortest output and/or prevents corruption.        if ($matchcount > strlen($str) / 3) {            //More than 1/3 of the content needs encoding, use B-encode.            $encoding = 'B';        } elseif ($matchcount > 0) {            //Less than 1/3 of the content needs encoding, use Q-encode.            $encoding = 'Q';        } elseif (strlen($str) > $maxlen) {            //No encoding needed, but value exceeds max line length, use Q-encode to prevent corruption.            $encoding = 'Q';        } else {            //No reformatting needed            $encoding = false;        }         switch ($encoding) {            case 'B':                if ($this->hasMultiBytes($str)) {                    //Use a custom function which correctly encodes and wraps long                    //multibyte strings without breaking lines within a character                    $encoded = $this->base64EncodeWrapMB($str, "\n");                } else {                    $encoded = base64_encode($str);                    $maxlen -= $maxlen % 4;                    $encoded = trim(chunk_split($encoded, $maxlen, "\n"));                }                $encoded = preg_replace('/^(.*)$/m', ' =?' . $charset . "?$encoding?\\1?=", $encoded);                break;            case 'Q':                $encoded = $this->encodeQ($str, $position);                $encoded = $this->wrapText($encoded, $maxlen, true);                $encoded = str_replace('=' . static::$LE, "\n", trim($encoded));                $encoded = preg_replace('/^(.*)$/m', ' =?' . $charset . "?$encoding?\\1?=", $encoded);                break;

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/PHPMailer/PHPMailer.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.