PHPMailer::encodeHeader( string $str, string $position = 'text' ): string
- Source
wp-includes/PHPMailer/PHPMailer.php:3723
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
- Scaling
- Constant
- Instructions
- 15–92
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 164.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- regexregular expression over the whole input
preg_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.
| When | Instructions | Calls it makes |
|---|---|---|
$position !== "comment" | 15 | strtolower(), ::normalizeBreaks() |
!$str | 26–33 | strtolower(), strtolower(), addcslashes() |
| always | 40–58 | strtolower(), strtolower(), preg_match_all(), ->has8bitChars() |
| always | 45–62 | strtolower(), strtolower(), preg_match_all(), preg_match_all(), ->has8bitChars() |
->hasMultiBytes() | 63–79 | strtolower(), strtolower(), preg_match_all(), ->has8bitChars(), ->hasMultiBytes(), ->base64EncodeWrapMB(), ::normalizeBreaks() |
->hasMultiBytes() | 68–83 | strtolower(), strtolower(), preg_match_all(), preg_match_all(), ->has8bitChars(), ->hasMultiBytes(), ->base64EncodeWrapMB(), ::normalizeBreaks() |
| always | 70–88 | strtolower(), strtolower(), preg_match_all(), ->has8bitChars(), ->encodeQ(), ->wrapText(), ::normalizeBreaks() |
!->hasMultiBytes() | 70–86 | strtolower(), strtolower(), preg_match_all(), ->has8bitChars(), ->hasMultiBytes(), base64_encode(), chunk_split(), ::normalizeBreaks() |
| always | 75–92 | strtolower(), strtolower(), preg_match_all(), preg_match_all(), ->has8bitChars(), ->encodeQ(), ->wrapText(), ::normalizeBreaks() |
!->hasMultiBytes() | 75–90 | strtolower(), strtolower(), preg_match_all(), preg_match_all(), ->has8bitChars(), ->hasMultiBytes(), base64_encode(), chunk_split(), ::normalizeBreaks() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 164 | 15–92 | 17 | |
| 8.5 | 164 | 15–92 | 17 | |
| 8.4 | 164 | 15–92 | 17 | 23 fewer instructions than PHP 8.3 |
| 8.3 | 187 | 17–102 | 17 | |
| 8.2 | 187 | 17–102 | 17 | |
| 8.1 | 187 | 17–102 | 17 | 1 more instruction than PHP 7.4 |
| 7.4 | 186 | 17–102 | 17 |
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.
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.