Variable Variables:
$vars = array('to', 'cc', 'bcc', 'from'); foreach ($vars as $varname) { if (isset($$varname)) { if (is_string($$varname)) { $$varname = explode(',', $$varname); } $$varname = array_filter(filter_var_array(preg_replace('~[<>]|%0[ab]|[[:cntrl:]]~i', '', $$varname), FILTER_VALIDATE_EMAIL)); } }
Normal (without using variable variables):
$vars = compact('to', 'cc', 'bcc', 'from'); foreach ($vars as $name => &$var) { if (is_string($var)) { $var = explode(',', $var); } $var = array_filter(filter_var_array(preg_replace('~[<>]|%0[ab]|[[:cntrl:]]~i', '', $var), FILTER_VALIDATE_EMAIL)); } extract ($vars);
Please note: you do not need isset, because compact will only import variables that are set. Everyone else is ignored ...
BTW: you do not need === true. isset () or is_string () always returns a boolean value. So === true is redundant ...
source share