TheDeanVanGreunen

example.php

Mar 9th, 2022
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.61 KB | None | 0 0
  1. $mystring = "red blue balloon";
  2.     $matches = null;
  3.     preg_match_all('/(red|blue|balloon)/uim', $mystring, $matches, PREG_OFFSET_CAPTURE);
  4.     $counts = $matches[0];
  5.     $red = 0;
  6.     $blue = 0;
  7.     $ballon = 0;
  8.     foreach ($counts as $count){
  9.         if($count[0] == "red"){
  10.             $red += 1;
  11.         } else if($count[0] == "blue"){
  12.             $blue += 1;
  13.         } else if($count[0] == "balloon"){
  14.             $ballon += 1;
  15.         }  
  16.     }
  17.    
  18.     $output = [
  19.         "red" => $red >= 1,
  20.         "blue" => $blue >= 1,
  21.         "red and blue" => $red >= 1 && $blue >= 1
  22.     ];
  23.     print_r($output);
Advertisement
Add Comment
Please, Sign In to add comment