fork download
  1. <?php // your code goes here
  2.  
  3. $texto = 'Header 1
  4. Codigo : c001
  5. Nombre : Juan
  6. Total : 45,78
  7.  
  8.  
  9.  
  10. Header 1
  11. c001
  12. Nombre : Juan
  13. Cantidad : 23
  14. Subtotal : 45.89
  15.  
  16. Total : 3410.67
  17.  
  18. Header 1
  19. Codigo : c002
  20. Nombre : Ana
  21. Total : 45.89
  22. Header 1
  23. c001
  24. Nombre : Juan
  25. Cantidad:4';
  26.  
  27.  
  28.  
  29. // Coincidir con el regex
  30. $regex = '/^Header 1$(?:\R(?!Header ).*+)*?\RNombre : Juan$(?:\R(?!\R*Header ).*+)*/mi';
  31.  
  32. if (preg_match_all($regex, $texto, $resultado)) {
  33. //mostrar secciones
  34. $n = 0;
  35. foreach ($resultado[0] as &$seccion) {
  36. echo "\n-----Seccion " . ++$n . "-----\n";
  37. echo $seccion;
  38. }
  39. } else {
  40. echo "No se encontrĂ³ el nombre";
  41. }
Success #stdin #stdout 0.02s 23764KB
stdin
Standard input is empty
stdout
-----Seccion 1-----
Header 1
Codigo : c001
Nombre : Juan
Total  : 45,78
-----Seccion 2-----
Header 1
c001
Nombre : Juan
Cantidad : 23
Subtotal : 45.89

Total     : 3410.67
-----Seccion 3-----
Header 1
c001
Nombre : Juan
Cantidad:4