Contao\StringUtil::extractEmail PHP Метод

extractEmail() публичный статический Метод

Extract all e-mail addresses from a string
public static extractEmail ( string $strString, string $strAllowedTags = '' ) : array
$strString string The string
$strAllowedTags string A list of allowed HTML tags
Результат array The e-mail addresses
    public static function extractEmail($strString, $strAllowedTags = '')
    {
        $arrEmails = array();
        if (strpos($strString, '@') === false) {
            return $arrEmails;
        }
        // Find all mailto: addresses
        preg_match_all('/mailto:(?:[^\\x00-\\x20\\x22\\x40\\x7F]+|\\x22[^\\x00-\\x1F\\x7F]+?\\x22)@(?:\\[(?:IPv)?[a-f0-9.:]+\\]|[\\w.-]+\\.[a-z]{2,63}\\b)/u', $strString, $matches);
        foreach ($matches[0] as &$strEmail) {
            $strEmail = str_replace('mailto:', '', $strEmail);
            if (\Validator::isEmail($strEmail)) {
                $arrEmails[] = $strEmail;
            }
        }
        // Encode opening arrow brackets (see #3998)
        $strString = preg_replace_callback('@</?([^\\s<>/]*)@', function ($matches) use($strAllowedTags) {
            if ($matches[1] == '' || strpos(strtolower($strAllowedTags), '<' . strtolower($matches[1]) . '>') === false) {
                $matches[0] = str_replace('<', '&lt;', $matches[0]);
            }
            return $matches[0];
        }, $strString);
        // Find all addresses in the plain text
        preg_match_all('/(?:[^\\x00-\\x20\\x22\\x40\\x7F]+|\\x22[^\\x00-\\x1F\\x7F]+?\\x22)@(?:\\[(?:IPv)?[a-f0-9.:]+\\]|[\\w.-]+\\.[a-z]{2,63}\\b)/u', strip_tags($strString), $matches);
        foreach ($matches[0] as &$strEmail) {
            $strEmail = str_replace('&lt;', '<', $strEmail);
            if (\Validator::isEmail($strEmail)) {
                $arrEmails[] = $strEmail;
            }
        }
        return array_unique($arrEmails);
    }

Usage Example

Пример #1
0
    /**
     * Tests the StringUtil::extactEmail() method.
     */
    public function testExtractEmail()
    {
        $text = <<<EOF
This is a [email protected] and this a [email protected]. Another [email protected] and also a [email protected] or an [email protected]. There are "*****@*****.**"@example.com and "very.(),:;<>[]\\".VERY.\\"very@\\ \\"very\\".unusual"@strange.example.com and even !#\$%&'*+-/=?^_`{}|[email protected] or "()<>[]:,;@\\"!#\$%&'*+-/=?^_`{}|~.a"@example.org but they are all valid.
IP addresses as in user@[255.255.255.255], user@[IPv6:2001:db8:1ff::a0b:dbd0], user@[IPv6:2001:0db8:85a3:08d3:1319:8a2e:0370:7344], user@[IPv6:2001::7344] or user@[IPv6:1111:2222:3333:4444:5555:6666:255.255.255.255] are valid, too.
We also support IDNA domains as in test@exämple.com, test@ä-.xe, [email protected], test@wähwähwäh.ümläüts.de or "tes@t"@wähwähwäh.ümläüts.de. And we support new TLDs as in [email protected] or [email protected].
And we support unicode characters in the local part (RFC 6531) as in niceä[email protected], véry.ç[email protected], a.lîttle.lengthy.but.fiñ[email protected], dîsposable.style.é[email protected], other.é[email protected], "verî.uñ[email protected]ñusual.com"@example.com, "verî.(),:;<>[]\\".VERÎ.\\"verî@\\ \\"verî\\".unüsual"@strange.example.com, üñîçøðé@example.com, "üñîçøðé"@example.com or DžǼ੧ఘⅧ⒇৪@example.com.
Of course also with IP addresses: üser@[255.255.255.255], üser@[IPv6:2001:db8:1ff::a0b:dbd0], üser@[IPv6:2001:0db8:85a3:08d3:1319:8a2e:0370:7344], üser@[IPv6:2001::7344] or üser@[IPv6:1111:2222:3333:4444:5555:6666:255.255.255.255] and unicode characters in the local part: tést@exämple.com, tést@ä-.xe, té[email protected], tést@wähwähwäh.ümläüts.de, "tés@t"@wähwähwäh.ümläüts.de. New TLDs? No problem: té[email protected] or té[email protected].
And hopefully we do not match invalid addresses such as [email protected], [email protected], test@_smtp_.example.com, [email protected], test@subexamplecom, Abc.example.com, a"b(c)d,e:f;gi[j\\k][email protected], this is"not\\[email protected], this\\ still\\"not\\[email protected], (comment)[email protected], test@[1.2.3.4, @ or test@.
EOF;
        $expected = ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '"*****@*****.**"@example.com', '"very.(),:;<>[]\\".VERY.\\"very@\\ \\"very\\".unusual"@strange.example.com', '!#$%&\'*+-/=?^_`{}|[email protected]', '"()<>[]:,;@\\"!#$%&\'*+-/=?^_`{}|~.a"@example.org', 'user@[255.255.255.255]', 'user@[IPv6:2001:db8:1ff::a0b:dbd0]', 'user@[IPv6:2001:0db8:85a3:08d3:1319:8a2e:0370:7344]', 'user@[IPv6:2001::7344]', 'user@[IPv6:1111:2222:3333:4444:5555:6666:255.255.255.255]', 'test@exämple.com', 'test@ä-.xe', '*****@*****.**', 'test@wähwähwäh.ümläüts.de', '"tes@t"@wähwähwäh.ümläüts.de', '*****@*****.**', '*****@*****.**', 'niceä[email protected]', 'véry.ç[email protected]', 'a.lîttle.lengthy.but.fiñ[email protected]', 'dîsposable.style.é[email protected]', 'other.é[email protected]', '"verî.uñ[email protected]ñusual.com"@example.com', '"verî.(),:;<>[]\\".VERÎ.\\"verî@\\ \\"verî\\".unüsual"@strange.example.com', 'üñîçøðé@example.com', '"üñîçøðé"@example.com', 'DžǼ੧ఘⅧ⒇৪@example.com', 'üser@[255.255.255.255]', 'üser@[IPv6:2001:db8:1ff::a0b:dbd0]', 'üser@[IPv6:2001:0db8:85a3:08d3:1319:8a2e:0370:7344]', 'üser@[IPv6:2001::7344]', 'üser@[IPv6:1111:2222:3333:4444:5555:6666:255.255.255.255]', 'tést@exämple.com', 'tést@ä-.xe', 'té[email protected]', 'tést@wähwähwäh.ümläüts.de', '"tés@t"@wähwähwäh.ümläüts.de', 'té[email protected]', 'té[email protected]'];
        $actual = StringUtil::extractEmail($text);
        sort($actual);
        sort($expected);
        $this->assertEquals($expected, $actual);
    }