choval / email_parse
Email parse and validation functions for PHP
v1.0.1
2023-11-19 21:18 UTC
Requires (Dev)
- phpunit/phpunit: ^9
README
Email functions for validating and parsing emails.
Why
- PHP's
filter_var
doesn't validate Unicode emails. - Punycode~
- Why not?
Install
composer require choval/email_parse
Functions
email_parse
Parses an email and returns an array with the parts of an email.
$parts = email_parse('user@EXAMPLE.COM'); var_dump($parts); /* array(11) { ["raw"]=> string(16) "user@EXAMPLE.COM" ["unicode"]=> bool(false) ["valid"]=> bool(true) ["idn_domain"]=> string(11) "example.com" ["idn_local"]=> string(4) "user" ["idn_tld"]=> string(3) "com" ["domain"]=> string(11) "example.com" ["tld"]=> string(3) "com" ["local"]=> string(4) "user" ["safe_email"]=> string(16) "user@example.com" ["email"]=> string(16) "user@example.com" } */ // Go crazy $parts = email_parse('π@ν μ€νΈγγγΉγ'); var_dump($parts); /* array(11) { ["raw"]=> string(26) "π@ν μ€νΈγγγΉγ" ["unicode"]=> bool(true) ["valid"]=> bool(true) ["idn_domain"]=> string(25) "xn--9t4b11yi5a.xn--zckzah" ["idn_local"]=> string(8) "xn--h28h" ["idn_tld"]=> string(10) "xn--zckzah" ["domain"]=> string(19) "ν μ€νΈ.γγΉγ" ["tld"]=> string(9) "γγΉγ" ["local"]=> string(4) "π" ["safe_email"]=> string(34) "xn--h28h@xn--9t4b11yi5a.xn--zckzah" ["email"]=> string(24) "π@ν μ€νΈ.γγΉγ" } */
email_valid
Check if an email is valid and returns the email if valid or false.
Uses email_parse
in the background and returns email
key.
$valid = email_valid('π@ν μ€νΈγγγΉγ'); var_dump($valid); // string(24) "π@ν μ€νΈ.γγΉγ" // Non valid because of starting dot $valid = email_valid('.π@ν μ€νΈ.γγΉγ'); var_dump($valid); // bool(false)
email_safe
Check if an email is valid and returns the email in punycode if valid or false.
Uses email_parse
in the background and returns safe_email
key.
$valid = email_safe('π@ν μ€νΈγγγΉγ'); var_dump($valid); // string(34) "xn--h28h@xn--9t4b11yi5a.xn--zckzah" // Non valid because of starting dot $valid = email_safe('.π@ν μ€νΈ.γγΉγ'); var_dump($valid); // bool(false)
License
MIT