From 10319c723df31ab1a0cb8bbd75d6bae9ba403767 Mon Sep 17 00:00:00 2001 From: Wruczek Date: Wed, 11 Jul 2018 03:53:04 +0200 Subject: [PATCH] Updated PHP code snippet - Optimised and cleaned, faster by about 32.5% on my machine - Added optional parameter to select the blacklist file, defaults to "disposable_email_blacklist.conf" file in the same directory as script - Renamed and indented according to PSR-2 --- README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1d36e7f..91f07ec 100644 --- a/README.md +++ b/README.md @@ -31,14 +31,16 @@ Available as [PyPI module](https://pypi.org/project/disposable-email-domains) th True ``` -**PHP** contributed by [@txt3rob](https://github.com/txt3rob), [@deguif](https://github.com/deguif) and [@pjebs](https://github.com/pjebs) +**PHP** contributed by [@txt3rob](https://github.com/txt3rob), [@deguif](https://github.com/deguif), [@pjebs](https://github.com/pjebs) and [@Wruczek](https://github.com/Wruczek) + +1. Make sure the passed email is valid. You can check that with [filter_var](https://secure.php.net/manual/en/function.filter-var.php) +2. Make sure you have the mbstring extension installed on your server ```php -function is_disposable_email($email) { - $path = realpath(dirname(__FILE__)) . '/disposable_email_blacklist.conf'; - $mail_domains_ko = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); - $mail_domains_ko = array_fill_keys($mail_domains_ko, true); - $domain = mb_strtolower(explode('@', trim($email))[1]); - return (isset($mail_domains_ko[$domain]) || array_key_exists($domain, $mail_domains_ko)); +function isDisposableEmail($email, $blacklist_path = null) { + if (!$blacklist_path) $blacklist_path = __DIR__ . '/disposable_email_blacklist.conf'; + $disposable_domains = file($blacklist_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + $domain = mb_strtolower(explode('@', trim($email))[1]); + return in_array($domain, $disposable_domains); } ``` **Ruby on Rails** contributed by [@MitsunChieh](https://github.com/MitsunChieh)