2

Here are the 9 different Strings that I want to apply if else conditions(or something better) for:

1. Automated - Under the Hood: iPhone 6 running iOS 9
2. Automated - AU/NZ:  iPhone 6 running iOS 9
3. Automated - DRM: iPhone 6 running iOS 9 
4. Automated - Ads regression: iPhone6 running iOS 9
5. Automated - Ads regression: iPhone 5 running iOS 8 
6. Automated - UI & Functional Regression on iPad 2 running iOS 8
7. Automated - Under the Hood: iPad Air on iOS 8
8. Automated - AU/NZ: iPad Air running iOS 8
9. Automated - DRM iPadAir running iOS 8

Every time, I will get one of above string and that will be assigned to a PHP variable. It is possible that the order of words or position of few words in any string can change. But certain things like OS, phone type, OS version and test type will not change. Based on what specific words, a string contains, I will do a REST api call.

For example, if PHP var is holding the 1st string i.e if

$test = Automated - Under the Hood: iPhone 6 running iOS 9, then I will make #1 rest api call.

So, the way I think I should do is this:

if ($test/string contains "under", "hood", "iPhone 6", "iOS 9") then do #1rest call (that i have command for)

else if ($test/string contains "AU", "NZ", "iPhone 6", "iOS 9") then do #2rest call..
....
else
... for all the above 9 strings. 

So far I have tried .contains() in PHP. Will a switch be a better alternative to this? If yes, an example would be good to have. Otherwise, better way to do in if-else conditions / regex.

4

1 Answer 1

1

You can use a named capturing group with the following regex:

-\s*(?<what>.*):.*?(?<device>ip(?:hone|ad(?: ?air)?))\s*(?<model>\d)?.*ios\s*(?<os>\d)

You can try it there at regex101

If you want to run PHP:

$re = "/-\s*(?<what>.*):.*?(?<device>ip(?:hone|ad(?: ?air)?))\s*(?<model>\d)?.*ios\s*(?<os>\d)/mi"; 
$strs = array(
    "1. Automated - Under the Hood: iPhone 6 running iOS 9",
    "2. Automated - AU/NZ:  iPhone 6 running iOS 9",
    "3. Automated - DRM: iPhone 6 running iOS 9 ",
    "4. Automated - Ads regression: iPhone6 running iOS 9",
    "5. Automated - Ads regression: iPhone 5 running iOS 8 ",
    "6. Automated - UI & Functional Regression: on iPad 2 running iOS 8",
    "7. Automated - Under the Hood: iPad Air on iOS 8",
    "8. Automated - AU/NZ: iPad Air running iOS 8",
    "9. Automated - DRM: iPadAir running iOS 8"
); 

foreach($strs as $str)
{
    preg_match_all($re, $str, $matches);
    echo "What: " . $matches['what'][0] . "\n";
    echo "Device: " . $matches['device'][0] . "\n";
    echo "Device version: " . $matches['model'][0] . "\n";
    echo "Os: " . $matches['os'][0] . "\n";   
}

Then you can use matches['nameOfGroup'][0] within an if condition (with method in_array for example) to execute the code you want to run.

Choosing between a switch or a big if/else condition will depend of you code and the method you want to execute depending of the results.

Not the answer you're looking for? Browse other questions tagged or ask your own question.