fork download
  1. <?php
  2.  
  3. $str = 'You have moved left 55 spaces. You have moved right 23 spaces. You have moved left 71 spaces. You have jumped up. You have moved left 2 spaces. You have moved left 88 spaces. You have jumped down. You have jumped up. You have moved right 131 spaces. You have jumped down.';
  4.  
  5. $directions = array('left', 'right');
  6.  
  7. $directions_regex = implode('|', array_map('preg_quote', $directions));
  8.  
  9. preg_match_all("~($directions_regex)\s+(\d+)~", $str, $matches);
  10.  
  11. var_dump($matches);
Success #stdin #stdout 0s 20520KB
stdin
Standard input is empty
stdout
array(3) {
  [0]=>
  array(6) {
    [0]=>
    string(7) "left 55"
    [1]=>
    string(8) "right 23"
    [2]=>
    string(7) "left 71"
    [3]=>
    string(6) "left 2"
    [4]=>
    string(7) "left 88"
    [5]=>
    string(9) "right 131"
  }
  [1]=>
  array(6) {
    [0]=>
    string(4) "left"
    [1]=>
    string(5) "right"
    [2]=>
    string(4) "left"
    [3]=>
    string(4) "left"
    [4]=>
    string(4) "left"
    [5]=>
    string(5) "right"
  }
  [2]=>
  array(6) {
    [0]=>
    string(2) "55"
    [1]=>
    string(2) "23"
    [2]=>
    string(2) "71"
    [3]=>
    string(1) "2"
    [4]=>
    string(2) "88"
    [5]=>
    string(3) "131"
  }
}