<?php function getCode($string) { $code = 0; if (preg_match('/\bred\b/i', $string)) { $code += 2; } if (preg_match('/\bblue\b/i', $string)) { $code += 3; } if (preg_match('/\bgreen\b/i', $string)) { $code += 4; } if ($code === 9) { $code += 2; } if ($code) { return $code; } if (preg_match('/\bballoon\b/i', $string)) { return 1; } return 0; } $sentences = [ 'Here is a green, blue, and red balloon', 'A blue balloon here', 'A red herring', 'Blue is the name of my dog', 'Red and green are Xmas colors', 'blue skies over green grass', 'Foo is bar', 'Have a balloon and a balloon and a balloon', 'A green balloon', 'A blue balloon with a red string', ]; foreach ($sentences as $sentence) { printf("%s => %d\n", $sentence, getCode($sentence)); }
You have javascript disabled. You will not be able to edit any code.