wppaste
WordPress

PHPMailer::parseAddresses( string $addrstr, bool $useimap = true, string $charset = self::CHARSET_ISO88591 ): array

Source
wp-includes/PHPMailer/PHPMailer.php:1222
Parse and validate a string containing one or more RFC822-style comma-separated email addresses of the form "display name " into an array of name/address pairs.

Description

Uses the imap_rfc822_parse_adrlist function if the IMAP extension is available.
Note that quotes in the name part are removed.

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

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

Parameters

$addrstrstring
The address list string
$useimapbooloptional
Whether to use the IMAP extension to parse the listDefault: true
$charsetstringoptional
The charset to use when decoding the address list string.Default: self::CHARSET_ISO88591

Return value

array

Performance profile

How much work a call to PHPMailer::parseAddresses() 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
Scales with input

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

Instructions
13–20

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

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 · 3 distinct outcomes

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

WhenInstructionsCalls it makes
always13–14explode()
!function_exists()17–18function_exists(), explode()
function_exists()19–20function_exists(), imap_rfc822_parse_adrlist(), imap_errors()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14113–20175 more instructions than PHP 8.5
8.513613–2015
8.413613–201533 fewer instructions than PHP 8.3
8.316913–2015
8.216913–2015
8.116913–20151 fewer instruction than PHP 7.4
7.417013–2015

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

  • static::validateAddress()

Source code

    public static function parseAddresses($addrstr, $useimap = true, $charset = self::CHARSET_ISO88591)    {        $addresses = [];        if ($useimap && function_exists('imap_rfc822_parse_adrlist')) {            //Use this built-in parser if it's available            $list = imap_rfc822_parse_adrlist($addrstr, '');            // Clear any potential IMAP errors to get rid of notices being thrown at end of script.            imap_errors();            foreach ($list as $address) {                if (                    '.SYNTAX-ERROR.' !== $address->host &&                    static::validateAddress($address->mailbox . '@' . $address->host)                ) {                    //Decode the name part if it's present and encoded                    if (                        property_exists($address, 'personal') &&                        //Check for a Mbstring constant rather than using extension_loaded, which is sometimes disabled                        defined('MB_CASE_UPPER') &&                        preg_match('/^=\?.*\?=$/s', $address->personal)                    ) {                        $origCharset = mb_internal_encoding();                        mb_internal_encoding($charset);                        //Undo any RFC2047-encoded spaces-as-underscores                        $address->personal = str_replace('_', '=20', $address->personal);                        //Decode the name                        $address->personal = mb_decode_mimeheader($address->personal);                        mb_internal_encoding($origCharset);                    }                     $addresses[] = [                        'name' => (property_exists($address, 'personal') ? $address->personal : ''),                        'address' => $address->mailbox . '@' . $address->host,                    ];                }            }        } else {            //Use this simpler parser            $list = explode(',', $addrstr);            foreach ($list as $address) {                $address = trim($address);                //Is there a separate name part?                if (strpos($address, '<') === false) {                    //No separate name, just use the whole thing                    if (static::validateAddress($address)) {                        $addresses[] = [                            'name' => '',                            'address' => $address,                        ];                    }                } else {                    list($name, $email) = explode('<', $address);                    $email = trim(str_replace('>', '', $email));                    $name = trim($name);                    if (static::validateAddress($email)) {                        //Check for a Mbstring constant rather than using extension_loaded, which is sometimes disabled                        //If this name is encoded, decode it                        if (defined('MB_CASE_UPPER') && preg_match('/^=\?.*\?=$/s', $name)) {                            $origCharset = mb_internal_encoding();                            mb_internal_encoding($charset);                            //Undo any RFC2047-encoded spaces-as-underscores                            $name = str_replace('_', '=20', $name);                            //Decode the name                            $name = mb_decode_mimeheader($name);                            mb_internal_encoding($origCharset);                        }                        $addresses[] = [                            //Remove any surrounding quotes and spaces from the name                            'name' => trim($name, '\'" '),                            'address' => $email,                        ];                    }                }            }        }         return $addresses;    }

Changelog

2 changes between 6.7.10 and 7.1.3.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

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

7.1.3
Parameter $useimap retyped from null to bool|null.verified against source
6.9.10
Parameter $useimap retyped from bool to null.verified against source

About this page

Parsed data
Generated from the wordpress-develop 6.8.11 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.