1

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?

whatscool
  • 317
  • 2
  • 18
  • 1
    Where are all those `$stringfilterX` variables coming from? – Don't Panic May 23 '17 at 15:35
  • would this be better for https://codereview.stackexchange.com/ ? – treyBake May 23 '17 at 15:37
  • 1
    you should think about rewriting $stringfilterX vars and convert them into an array in order to make them iterable. There's a way indeed to iterate through them as well as vars by using $$ operator and making a for statement from 1 to N ($pattern="stringfilter"; for($i=1;$i<=N;$i++) {$variable_name=$pattern.$i; print " value in variable ".$variable_name.": ".$$variable_name; }) but it's absolutely horror stuff and the most dirty code you can write – Ace_Gentile May 23 '17 at 15:39
  • 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 ); – whatscool May 23 '17 at 15:44
  • 1
    If it's on your hand to modify the form, take a took at https://stackoverflow.com/questions/20184670/html-php-form-input-as-array to get your data as Array – Jordi Nebot May 23 '17 at 15:46
  • Is it possible to define the text boxes on the other page as an array? That would really simplify this. – Don't Panic May 23 '17 at 15:57

2 Answers2

3

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;
    }
}
Ethan
  • 4,295
  • 4
  • 25
  • 44
1

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80