fork download
  1. <?php
  2.  
  3. $tests = [
  4. "0000 0000 0000 0000 00/0000 000", // válido
  5. "0000000000000000 00/0000 000", // válido
  6. "00000000 00000000 00/0000 000", // válido
  7. "0000000 000000000 00/0000 000", // inválido: espaços errados
  8. "0000 000 0000 0000 00/0000 000", // inválido: tamanho da sequência errada
  9. "000000000000000 00/0000 000", // inválido: tamanho da sequência errada
  10. ];
  11.  
  12. foreach($tests as $test)
  13. {
  14. if (preg_match("/^((\d{4}\s?){3}\d{4}) (\d{2}\/\d{4}) (\d{3})$/", $test, $matches))
  15. {
  16. print_r($matches);
  17. }
  18. }
Success #stdin #stdout 0.04s 23748KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 0000 0000 0000 0000 00/0000 000
    [1] => 0000 0000 0000 0000
    [2] => 0000 
    [3] => 00/0000
    [4] => 000
)
Array
(
    [0] => 0000000000000000 00/0000 000
    [1] => 0000000000000000
    [2] => 0000
    [3] => 00/0000
    [4] => 000
)
Array
(
    [0] => 00000000 00000000 00/0000 000
    [1] => 00000000 00000000
    [2] => 0000
    [3] => 00/0000
    [4] => 000
)