fork download
  1. <?php
  2.  
  3. $re = '/(\w+) (subiu|caiu) (?:.*?) (\d+m)/';
  4. $str = 'O gato subiu no telhado de 10m, um homem jogou-lhe uma pedra e ele caiu de uma altura de 20m.';
  5.  
  6. preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
  7.  
  8. foreach($matches as $match) {
  9. var_dump(implode(' ', array_slice($match, 1)));
  10. }
  11.  
  12. // Saídas:
  13. // string(14) "gato subiu 10m"
  14. // string(12) "ele caiu 20m"
Success #stdin #stdout 0.01s 82944KB
stdin
Standard input is empty
stdout
string(14) "gato subiu 10m"
string(12) "ele caiu 20m"