Cleaning up a very long if statement

105 Views Asked by At

I have a very long list of strings called $stringfilter1 $stringfilter2 etc all the way up to $stringfilter50

I have another string $reporteremail and I want to make a conditional statement whereby if any of the $stringfilter strings is present in the $reporteremail, some code is executed. At the moment my code looks like this and it works:

if (stripos($reporteremail, $stringfilter1) !== false || stripos($reporteremail, $stringfilter2) !== false || stripos($reporteremail, $stringfilter3) !== false [...]) {
       runcode(); 
}

This is very very long though. I have cut it short here.

I was wondering if there's a cleaner, more efficient way to do this?

EDIT: I am writing a plugin for a bug tracker. The strings are entered on another page in text boxes. I access them on this page by running a function that looks like

$t_filter = plugin_config_get( 'filter1' ); 
$stringfilter1 = string_attribute( $t_filter1 );

I would agree looping through an array would be the best way to do this. How can I push each new string onto the end of an array without having to write that snippet above out 50 times?

2

There are 2 best solutions below

0
On

You don't need a loop. First put all your filters in an array instead of having them in separate variables. I would try to do this by modifying the input source rather than doing it in your PHP script. (Based on your comments I'm not sure if that's possible or not, so maybe you do need a loop like the one in the other answer.) Then you can use str_ireplace to check for your filter strings in the $reporteremail. (This will not modify $reporteremail.)

str_ireplace($filters, '', $reporteremail, $count);
if ($count) {
    // run code
}

The $count parameter will contain a count of how many replacements were performed. If it's nonzero, then at least one of the filters was found in $reporteremail.

0
On

How can I push each new string onto the end of an array without having to write that snippet above out 50 times?

Try this:

$needles = [];
for ($i = 0; $i < 50; $i++) {
    $t_filter = plugin_config_get("filter$i"); 
    $needles[] = string_attribute($t_filter);
}

I have a very long list of strings called $stringfilter1 $stringfilter2 etc all the way up to $stringfilter50

[...]

This is very very long though. I have cut it short here.

I was wondering if there's a cleaner, more efficient way to do this?

Try this, it should go after the code block above.

$flag = false;
foreach ($needles as $needle) {
    if (stripos($reporteremail, $needle) !== false) {
        $flag = true;
        break;
    }
}

if ($flag) {
    runcode();
}

The code above works by iterating through the $needles array and sets a flag if stripos doesn't return false. After it's finished iterating, it checks if the flag is true, if so, this means that one of the needles was found in the array.


EDIT Alternatively, you could do it all in one loop, which is both faster and more efficient.

$flag = false;
for ($i = 0; $i < 50; $i++) {
    $t_filter = plugin_config_get("filter$i"); 
    $needle   = string_attribute($t_filter);
    if (stripos($reporteremail, $needle) !== false) {
        // One of the needles was found in $reporteremail.
        runcode();
        break;
    }
}