/** * Tests the StringUtil::extactEmail() method. */ public function testExtractEmail() { $text = <<<EOF This is a niceandsimple@example.com and this a very.common@example.com. Another little.lengthy.but.fine@dept.example.com and also a disposable.style.email.with+symbol@example.com or an other.email-with-dash@example.com. There are "*****@*****.**"@example.com and "very.(),:;<>[]\\".VERY.\\"very@\\ \\"very\\".unusual"@strange.example.com and even !#\$%&'*+-/=?^_`{}|~@example.org 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, test@subexample.wizard, 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 test@example.photography or test@sub-domain.example.photography. And we support unicode characters in the local part (RFC 6531) as in niceändsimple@example.com, véry.çommon@example.com, a.lîttle.lengthy.but.fiñe@dept.example.com, dîsposable.style.émail.with+symbol@example.com, other.émail-with-dash@example.com, "verî.uñusual.@.uñ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ést@subexample.wizard, tést@wähwähwäh.ümläüts.de, "tés@t"@wähwähwäh.ümläüts.de. New TLDs? No problem: tést@example.photography or tést@sub-domain.example.photography. And hopefully we do not match invalid addresses such as test..child@example.com, test@sub.-example.com, test@_smtp_.example.com, test@sub..example.com, test@subexamplecom, Abc.example.com, a"b(c)d,e:f;gi[j\\k]l@example.com, this is"not\\allowed@example.com, this\\ still\\"not\\allowed@example.com, (comment)test@iana.org, test@[1.2.3.4, @ or test@. EOF; $expected = ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '"*****@*****.**"@example.com', '"very.(),:;<>[]\\".VERY.\\"very@\\ \\"very\\".unusual"@strange.example.com', '!#$%&\'*+-/=?^_`{}|~@example.org', '"()<>[]:,;@\\"!#$%&\'*+-/=?^_`{}|~.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ändsimple@example.com', 'véry.çommon@example.com', 'a.lîttle.lengthy.but.fiñe@dept.example.com', 'dîsposable.style.émail.with+symbol@example.com', 'other.émail-with-dash@example.com', '"verî.uñusual.@.uñ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ést@subexample.wizard', 'tést@wähwähwäh.ümläüts.de', '"tés@t"@wähwähwäh.ümläüts.de', 'tést@example.photography', 'tést@sub-domain.example.photography']; $actual = StringUtil::extractEmail($text); sort($actual); sort($expected); $this->assertEquals($expected, $actual); }