regex — Условия в PHP для строк, которые содержат определенные слова

Вот 9 разные строки, которые я хочу применить, если еще условия (or something better) за:

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

Каждый раз я получу один из above string и это будет назначено PHP variable, It is possible that the order of words or position of few words in any string can change, Но такие вещи, как OS, phone type, OS version and test type will not change, В зависимости от того, какие конкретные слова содержит строка, я сделаю вызов API REST.

Например, если PHP var содержит 1-ю строку, т.е. если

$test = Automated - Under the Hood: iPhone 6 running iOS 9, тогда я сделаю # 1 остальное вызов API.

Итак, я думаю, что должен сделать это:

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.

До сих пор я пробовал .contains () в PHP. Will a switch be a better alternative to this? If yes, an example would be good to have. В противном случае, лучший способ сделать это в условиях if / else / regex.

2

Решение

Вы можете использовать именованную группу захвата со следующим регулярным выражением:

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

Вы можете попробовать это там на regex101

Если вы хотите запустить 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";
}

Тогда вы можете использовать matches['nameOfGroup'][0] в пределах if условие (с методом in_array например) выполнить код, который вы хотите запустить.

Выбор между switch или большое условие if / else будет зависеть от вашего кода и метода, который вы хотите выполнить, в зависимости от результатов.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector