Regex d'un fichier texte et fromatage

Hippopocom
Invité n'ayant pas de compte PHPfrance

23 févr. 2011, 12:37

salut j'ai besoin de recuperé des données d'un fichier texte (voir exemple ci-dessous)

j'aimerai recuperé le tout sous cette forme :

Code : Tout sélectionner

array( array("TYPE"=>"Carte Normal","FORMAT"=>"Petit", "NOM"=>"Prénom Nom", "Fonction"=>"Assistant clientèle",.......etc ), array("TYPE"=>"Carte Normal","FORMAT"=>"Petit", "NOM"=>"Prénom Nom", "Fonction"=>"Assistant clientèle",.......etc ), array("TYPE"=>"Carte Normal","FORMAT"=>"Petit", "NOM"=>"Prénom Nom", "Fonction"=>"Assistant clientèle",.......etc ) );
comme ça apres je traite chaque fiche via un tableau :)

Le fichier (3 fiche):
===============================Type
TYPE : Carte Normale
FORMAT : Petit
NOM : Prénom nom
FONCTION :Assistant clientèle
Nom Agence : Agence de lens
ADRESSE : 5 rue de l'avanue
TEL : 0.800.000.000
FAX : 02.02.02.02.02
SERVICE TELEPHONE : 0 800 000 000
APPEL DEPUIS L' ETRANGER : +33 (0)2 02 02 02 02
SITE INTERNET : www.siteweb.fr

===============================Type
TYPE : Carte Normale
FORMAT : Petit
NOM : Prénom nom
FONCTION :Assistant clientèle
Nom Agence : Agence de lens
ADRESSE : 5 rue de l'avanue
TEL : 0.800.000.000
FAX : 02.02.02.02.02
SERVICE TELEPHONE : 0 800 000 000
APPEL DEPUIS L' ETRANGER : +33 (0)2 02 02 02 02
SITE INTERNET : www.siteweb.fr

===============================Type
TYPE : Carte Normale
FORMAT : Petit
NOM : Prénom nom
FONCTION :Assistant clientèle
Nom Agence : Agence de lens
ADRESSE : 5 rue de l'avanue
TEL : 0.800.000.000
FAX : 02.02.02.02.02
SERVICE TELEPHONE : 0 800 000 000
APPEL DEPUIS L' ETRANGER : +33 (0)2 02 02 02 02
SITE INTERNET : www.siteweb.fr

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

23 févr. 2011, 17:07

salut,

essai
<?php
$file = file_get_contents('hippo.txt');
$fiches = explode('===============================Type',$file);
$listedesfiches = array();
foreach ($fiches as $key => $fiche ) {
    if (strlen(trim($fiche)) !== 0) {
        $line = explode ("\n",$fiche);
        $a = array();
        foreach ($line as $l ){
            if (strlen(trim($l)) !== 0) {
            $uneligne = explode (':',$l);
            $a[ trim($uneligne[0])] = trim($uneligne[1]);
            }    
        }
        if (count($a) !== 0) {
                $listedesfiches[] = $a;
            }
    }
    
}

var_dump($listedesfiches);

?>
ce qui donne
array
0 =>
array
'TYPE' => string 'Carte Normale' (length=13)
'FORMAT' => string 'Petit' (length=5)
'NOM' => string 'Prénom nom' (length=10)
'FONCTION' => string 'Assistant clientèle' (length=19)
'Nom Agence' => string 'Agence de lens' (length=14)
'ADRESSE' => string '5 rue de l'avanue' (length=17)
'TEL' => string '0.800.000.000' (length=13)
'FAX' => string '02.02.02.02.02' (length=14)
'SERVICE TELEPHONE' => string '0 800 000 000' (length=13)
'APPEL DEPUIS L' ETRANGER' => string '+33 (0)2 02 02 02 02' (length=20)
'SITE INTERNET' => string 'www.siteweb.fr' (length=14)
1 =>
array
'TYPE' => string 'Carte Normale' (length=13)
'FORMAT' => string 'Petit' (length=5)
'NOM' => string 'Prénom nom' (length=10)
'FONCTION' => string 'Assistant clientèle' (length=19)
'Nom Agence' => string 'Agence de lens' (length=14)
'ADRESSE' => string '5 rue de l'avanue' (length=17)
'TEL' => string '0.800.000.000' (length=13)
'FAX' => string '02.02.02.02.02' (length=14)
'SERVICE TELEPHONE' => string '0 800 000 000' (length=13)
'APPEL DEPUIS L' ETRANGER' => string '+33 (0)2 02 02 02 02' (length=20)
'SITE INTERNET' => string 'www.siteweb.fr' (length=14)
2 =>
array
'TYPE' => string 'Carte Normale' (length=13)
'FORMAT' => string 'Petit' (length=5)
'NOM' => string 'Prénom nom' (length=10)
'FONCTION' => string 'Assistant clientèle' (length=19)
'Nom Agence' => string 'Agence de lens' (length=14)
'ADRESSE' => string '5 rue de l'avanue' (length=17)
'TEL' => string '0.800.000.000' (length=13)
'FAX' => string '02.02.02.02.02' (length=14)
'SERVICE TELEPHONE' => string '0 800 000 000' (length=13)
'APPEL DEPUIS L' ETRANGER' => string '+33 (0)2 02 02 02 02' (length=20)
'SITE INTERNET' => string 'www.siteweb.fr' (length=14)
Ps : dsl pour le 1er post foireux ^^

@+
Il en faut peu pour être heureux ......