How can I simplify this redundant code?

Can someone help me simplify 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 to use variable variables, but to no avail (this has been a long time since I used them).

+4
source share
7 answers

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 ...

+4
source

You can do (untested)

 $vars = array($from, $to, $cc, $bcc); foreach ($vars as $var) { $var = explode(',', $var); .... ... } $from = $vars[0]; $to = $vars[1]; $cc = $vars[2]; $bcc = $vars[3]; 
+2
source

Put it in a function?

 function validate($str) { if (isset($str) === true) { if (is_string($str) === true) { $str = explode(',', $str); } $str = array_filter(filter_var_array(preg_replace('~[<>]|%0[ab]|[[:cntrl:]]~i', '', $str), FILTER_VALIDATE_EMAIL)); } return $str; } $to = validate($to); $cc = validate($cc); $bcc = validate($bcc); $from = validate($from); 
+2
source

I could do this:

Perhaps you can create a function for this:

 function checkIt($var) { if (isset($var) === true) { if (is_string($var) === true) { $var = explode(',', $var); } $to = explode(',', $var); $to = array_filter(filter_var_array(preg_replace('~[<>]|%0[ab]|[[:cntrl:]]~i', '', $to), FILTER_VALIDATE_EMAIL)); } return $to; } 

And now you can pass your variables to this function.

+1
source

Just paste the values ​​into the array and go through it.

 function cleanEmails($value) { if (is_string($value)) { $value = explode(',', $value); } return array_filter(filter_var_array(preg_replace('~[<>]|%0[ab]|[[:cntrl:]]~i', '', $value), FILTER_VALIDATE_EMAIL)); } $fields = array(); if (isset($to)) { $fields['to'] = $to; } if (isset($from)) { $fields['from'] = $from; } if (isset($cc)) { $fields['cc'] = $cc; } if (isset($bcc)) { $fields['bcc'] = $bcc; } $result = array_map('cleanEmails', $fields); 

The end result will be a 2-dimensional array, the first index will be the fields that were set, the second index will be the corresponding email addresses ...

+1
source

Even without going through the full path of variable variables, you can simplify this by simply putting checks in a common function, and then do:

 $to = cleanup_email_addrs($to); $cc = cleanup_email_addrs($cc); $bcc = cleanup_email_addrs($bcc); $from = cleanup_email_addrs($from); 
+1
source

For starters, you can get rid of isset() === true ; isset() returns either true or false .

And of course, enable the function, since all your if seem to do the same, but this has been mentioned before ...

By the way, does your array_filter string array_filter if the input is not array() ?

If not, you need to include this statement in the if (is_string()) .

+1
source

Source: https://habr.com/ru/post/1310496/


All Articles