wppaste
WordPress

PHPMailer::wrapText( string $message, int $length, bool $qp_mode = false ): string

Source
wp-includes/PHPMailer/PHPMailer.php:2682
Word-wrap message.

Description

For use with mailers that do not automatically perform wrapping and for quoted-printable encoded messages.
Original written by philippe.

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

$messagestring
The message to wrap
$lengthint
The line length to wrap to
$qp_modebooloptional
Whether to run in Quoted-Printable modeDefault: false

Return value

string

Performance profile

How much work a call to PHPMailer::wrapText() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
34–41

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

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 one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always34–41strtolower(), ::normalizeBreaks(), explode()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15734–4122
8.515734–4122
8.415734–412240 fewer instructions than PHP 8.3
8.319737–5122
8.219737–51221 more instruction than PHP 8.1
8.119637–5122
7.419637–5122

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 · 2

  • static::normalizeBreaks()
  • \PHPMailer\PHPMailer\PHPMailer::utf8CharBoundary()

Source code

    public function wrapText($message, $length, $qp_mode = false)    {        if ($qp_mode) {            $soft_break = sprintf(' =%s', static::$LE);        } else {            $soft_break = static::$LE;        }        //If utf-8 encoding is used, we will need to make sure we don't        //split multibyte characters when we wrap        $is_utf8 = static::CHARSET_UTF8 === strtolower($this->CharSet);        $lelen = strlen(static::$LE);        $crlflen = strlen(static::$LE);         $message = static::normalizeBreaks($message);        //Remove a trailing line break        if (substr($message, -$lelen) === static::$LE) {            $message = substr($message, 0, -$lelen);        }         //Split message into lines        $lines = explode(static::$LE, $message);        //Message will be rebuilt in here        $message = '';        foreach ($lines as $line) {            $words = explode(' ', $line);            $buf = '';            $firstword = true;            foreach ($words as $word) {                if ($qp_mode && (strlen($word) > $length)) {                    $space_left = $length - strlen($buf) - $crlflen;                    if (!$firstword) {                        if ($space_left > 20) {                            $len = $space_left;                            if ($is_utf8) {                                $len = $this->utf8CharBoundary($word, $len);                            } elseif ('=' === substr($word, $len - 1, 1)) {                                --$len;                            } elseif ('=' === substr($word, $len - 2, 1)) {                                $len -= 2;                            }                            $part = substr($word, 0, $len);                            $word = substr($word, $len);                            $buf .= ' ' . $part;                            $message .= $buf . sprintf('=%s', static::$LE);                        } else {                            $message .= $buf . $soft_break;                        }                        $buf = '';                    }                    while ($word !== '') {                        if ($length <= 0) {                            break;                        }                        $len = $length;                        if ($is_utf8) {                            $len = $this->utf8CharBoundary($word, $len);                        } elseif ('=' === substr($word, $len - 1, 1)) {                            --$len;                        } elseif ('=' === substr($word, $len - 2, 1)) {                            $len -= 2;                        }                        $part = substr($word, 0, $len);                        $word = (string) substr($word, $len);                         if ($word !== '') {                            $message .= $part . sprintf('=%s', static::$LE);                        } else {                            $buf = $part;                        }                    }                } else {                    $buf_o = $buf;                    if (!$firstword) {                        $buf .= ' ';                    }                    $buf .= $word;                     if ('' !== $buf_o && strlen($buf) > $length) {                        $message .= $buf_o . $soft_break;                        $buf = $word;

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.