fork download
  1. <?php
  2.  
  3. $pattern = '~(?|(?=hello 2)(hello 2)|hello (1))~';
  4.  
  5.  
  6. $subjects = [];
  7. $subjects[] = <<<EOD
  8. test hello 2 test
  9. EOD;
  10.  
  11.  
  12. $subjects[] = <<<EOD
  13. test hello 1 test
  14. EOD;
  15.  
  16.  
  17. $result = preg_match_all($pattern, $subjects[0], $matches);
  18. assert($matches[1][0] == 'hello 2');
  19.  
  20. print_r($matches);
  21. $result = preg_match_all($pattern, $subjects[1], $matches);
  22. assert($matches[1][0] == '1');
  23.  
  24. print_r($matches);
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [0] => hello 2
        )

    [1] => Array
        (
            [0] => hello 2
        )

)
Array
(
    [0] => Array
        (
            [0] => hello 1
        )

    [1] => Array
        (
            [0] => 1
        )

)