Home > Software engineering >  extract specific data from .txt file using regex function
extract specific data from .txt file using regex function

Time:02-15

i want to extract some specific data from a txt file with regex, the file look like this :

Facture
F2107 98680  // ligne to be extracted
02072021
Emetteur
Adresse de facturation
incwo
16 rue de La Comte
75007 Paris
France
mon entreprise   846870
0786259448
773 vieille route
69490 Vindry sur turdine
France
TVA Intra EN FORMATION
Origine Bon de commande C2107 111476 du 20721
Designation
Prix unit
HT
Qte
Prix total
HT
TVA
2400  // ligne to be extracted
1
2400
20 A
Abonnement Basique   72021
Conditions de paiement
Modalites Penalites de retard  selon articles L441 10 et suivants du code du commerce taux applique  200 par an Une indemnite
forfaitaire de 40  sera due de plein droit ds le premier jour de retard de paiement Article D 441 5 du code du commerce Aucun
escompte ne sera accorde en cas de paiement anticipe
Coordonnees bancaires
Montant total lignes HT
2400
BANQUE
Caisse dAEpargne
CE ILE DE FRANCE
42 rue de Bretagne
75003 Paris   France
Code TVA

i can extract them by calling each line such an array : exmaple : echo $lines[20];

but sometimes the document lignes ares changes so i need to extract them dynamically , i found that regex could help in this situation and i tried but im stacked at this code :

<?php
$file = file_get_contents('./incwo.txt', true);
$lines = explode( "\n" , $file);
$charge_infos['detail']="INCWO";
$charge_infos['description'] = 'ACHATS DE MARCHANDISES';
for($i=0; $i<count($lines);$i   )
{
 $line=$lines[$i];

 if(preg_match('/facture/i', $line) || preg_match('/TVA/i', $line) ) {
 echo  nl2br ( $lines[$i  1] , "\n ");
 } 
}
?>

the result i get :

F2107 98680   // i need this line
Origine Bon de commande C2107 111476 du 20721
2400          // i need this line

A
Signature NF203
N dAactivite de centre de formation 11 75 55019 75 du 19082016

and thanks in advance

CodePudding user response:

If I understand your question correctly, you probably don't need regex for this. Try changing

if(preg_match('/facture/i', $line) || preg_match('/TVA/i', $line) )

to

if(($line == "Facture") || ($line == "TVA") ) 

and see if it works.

  • Related