choval/email_parse

Email parse and validation functions for PHP

v1.0.1 2023-11-19 21:18 UTC

This package is auto-updated.

Last update: 2024-10-19 23:18:31 UTC


README

Email functions for validating and parsing emails.

Why

  1. PHP's filter_var doesn't validate Unicode emails.
  2. Punycode~
  3. 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