fork download
  1. <?php
  2. $texto = "123 abc '456' def789'112' ghi";
  3. preg_match_all('/\'\d+\'|(\d+)/', $texto, $matches, PREG_SET_ORDER, 0);
  4. var_dump($matches);
  5.  
  6. foreach ($matches as $m) {
  7. if (count($m) > 1) { // grupo de captura preenchido (número não está entre aspas)
  8. echo $m[1]. "\n";
  9. }
  10. }
  11. // your code goes here
Success #stdin #stdout 0s 82624KB
stdin
Standard input is empty
stdout
array(4) {
  [0]=>
  array(2) {
    [0]=>
    string(3) "123"
    [1]=>
    string(3) "123"
  }
  [1]=>
  array(1) {
    [0]=>
    string(5) "'456'"
  }
  [2]=>
  array(2) {
    [0]=>
    string(3) "789"
    [1]=>
    string(3) "789"
  }
  [3]=>
  array(1) {
    [0]=>
    string(5) "'112'"
  }
}
123
789