Can someone please help me simpling this redundant piece of code?
if (isset($to) === true)
{
if (is_string($to) === true)
{
$to = explode(',', $to);
}
$to = array_filter(filter_var_array(preg_replace('~[<>]|%0[ab]|[[:cntrl:]]~i', '', $to), FILTER_VALIDATE_EMAIL));
}
if (isset($cc) === true)
{
if (is_string($cc) === true)
{
$cc = explode(',', $cc);
}
$cc = array_filter(filter_var_array(preg_replace('~[<>]|%0[ab]|[[:cntrl:]]~i', '', $cc), FILTER_VALIDATE_EMAIL));
}
if (isset($bcc) === true)
{
if (is_string($bcc) === true)
{
$bcc = explode(',', $bcc);
}
$bcc = array_filter(filter_var_array(preg_replace('~[<>]|%0[ab]|[[:cntrl:]]~i', '', $bcc), FILTER_VALIDATE_EMAIL));
}
if (isset($from) === true)
{
if (is_string($from) === true)
{
$from = explode(',', $from);
}
$from = array_filter(filter_var_array(preg_replace('~[<>]|%0[ab]|[[:cntrl:]]~i', '', $from), FILTER_VALIDATE_EMAIL));
}
I tried using variable variables but without success (it’s been a long time since I’ve used them).
Variable variables:
Regular (Without using variable variables):
Note, you don’t need isset, because compact will only import variables that are set. All others are ignored…
BTW: You don’t need the === true. isset() or is_string() will always return a boolean. So the === true is redundant…