IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Mettre en tableau un fichier texte


Sujet :

Langage PHP

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    412
    Détails du profil
    Informations personnelles :
    Âge : 52
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Décembre 2008
    Messages : 412
    Points : 79
    Points
    79
    Par défaut Mettre en tableau un fichier texte
    Bonjour

    voici mon problème,
    je ne connais pas très bien explode et je m'embrouille.

    J'ai un fichier "2012-07-rapport-ordres.log" sous cette forme
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    30/06/2012 -- 14:46:36 -- serveur -- cm15 E5 on -- minuterie.sh -- Aucun -- Script bash
    30/06/2012 -- 14:48:28 -- serveur -- cm15 E5 on -- minuterie.sh -- Aucun -- Script bash
    30/06/2012 -- 14:48:53 -- serveur -- cm15 E5 off -- minuterie.sh -- Aucun -- Script bash
    30/06/2012 -- 16:04:59 -- serveur -- cm15 E5 on -- minuterie.sh -- Macro minuterie E5 on/off -- Script bash
    30/06/2012 -- 18:53:25 -- serveur -- cm15 E1 on -- e1-15% --  Aucun -- Script bash
    30/06/2012 -- 18:59:15 -- serveur -- cm15 E1 off -- e1-off --  Aucun -- Script bash
    30/06/2012 -- 18:59:28 -- serveur -- cm15 E1 bri 15 -- e1-15% -- Aucun -- Script bash
    30/06/2012 -- 19:03:25 -- serveur -- cm15 E1 bri 15 -- e1-15% -- Aucun -- Script bash
    30/06/2012 -- 19:06:25 -- serveur -- cm15 E1 bri 15 -- e1-15% -- Aucun -- Script bash
    30/06/2012 -- 19:08:12 -- serveur -- cm15 E1 off -- e1-off -- Aucun -- Script bash
    30/06/2012 -- 22:38:17 -- serveur -- cm15 D8 off -- Macro e5-off vers d8-off -- Script bash
    22/07/2012 -- 21:17:12 -- root -- cm15 E1 on -- e1-on -- Aucun -- Script bash
    22/07/2012 -- 21:17:24 -- root -- cm15 E1 off -- e1-off -- Aucun -- Script bash
    22/07/2012 -- 23:45:24 -- root -- cm15 D14 off -- d14-off -- Aucun -- Script bash
    24/07/2012 -- 12:20:11 -- www-data -- cm15 D7 On -- b14-on -- Macro b14-on vers d7-on -- Script bash
    24/07/2012 -- 12:20:30 -- www-data -- cm15 D7 Off -- b14-off -- Macro b14-off vers d7-off -- Script bash
    24/07/2012 -- 13:19:19 -- www-data -- cm15 L4 On -- l4-on -- Macro l4-on vers f11-on -- Script bash
    24/07/2012 -- 13:19:33 -- www-data -- cm15 L4 Off -- l4-off -- Macro l4-off vers f11-off -- Script bash
    24/07/2012 -- 13:59:16 -- www-data -- cm15 E5 on -- e5-off -- Macro e5-on vers d8-on -- Script bash
    24/07/2012 -- 13:59:44 -- www-data -- cm15 E5 off -- e5-off -- Macro e5-off vers d8-off -- Script bash
    25/07/2012 -- 06:50:01 -- serveur -- cm15 D14 off -- D14-Off.sh -- Aucun -- Crontab
    25/07/2012 -- 14:03:16 -- www-data -- cm15 E1 on -- e1-on -- Aucun -- Script bash
    29/07/2012 -- 00:23:54 -- serveur -- cm15 E1 On -- e1-on -- Aucun -- Script bash
    29/07/2012 -- 01:03:44 -- serveur -- cm15 E1 On -- e1-on -- Aucun -- Script bash
    29/07/2012 -- 01:18:54 -- serveur -- cm15 E1 On -- e1-on -- Aucun -- Script bash
    29/07/2012 -- 02:01:00 -- serveur -- cm15 E1 On -- e1-on -- Aucun -- Script bash
    29/07/2012 -- 02:19:22 -- serveur -- cm15 E1 On -- e1-on -- Aucun -- Script bash
    je souhaiterais les ranger dans des tableaux sous la forme suivante : :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    date -- heure -- User -- Ordre X10 -- Fichier -- Macro -- executé par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <?php
     
     
    // le fichier
    $fichier1 = '/home/serveur/Domotique/rapports/ordres/2012-07-rapport-ordres.log';
    // ouverture du fichier
    $fp=fopen($fichier1,'r');
     
    while (!feof($fp)) {
    // lecture et decoupage des lignes à chaque ;
    $position=explode("-- ",fgets($fp,255));  
    // $position dans la ligne commence à zero
    $date.=$position[0];
    $heure.=$position[1];
    $user.=$position[2];
    $ordre_x10.=$position[3];
    $fichier_exec.=$position[4];
    $macro.=$position[5];
    $execute_par.=$position[6];
    }
     
    // fermeture du fichier
    fclose($fp);
    ?>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    <table style="text-align: left; width: 800px;" border="1"
    cellpadding="2" cellspacing="2">
    <tbody>
    <tr>
    <td style="vertical-align: top; width: 19px;">Date<br>
    </td>
    <td style="vertical-align: top; width: 19px;">Heure<br>
    </td>
    <td style="vertical-align: top; width: 1px;">User<br>
    </td>
    <td style="vertical-align: top; width: 80px;">Ordre x10<br>
    </td>
    <td style="vertical-align: top; width: 19px;">Fichier<br>
    </td>
    <td style="vertical-align: top; width: 91px;">Macro<br>
    </td>
    <td style="vertical-align: top; width: 100px;">Executé par<br>
    <!-- </td>
    </tr>
    <tr>
    <td style="vertical-align: top; width: 19px;"><br>
    </td>
    <td style="vertical-align: top; width: 19px;"><br>
    </td>
    <td style="vertical-align: top; width: 1px;"><br>
    </td>
    <td style="vertical-align: top; width: 80px;"><br>
    </td>
    <td style="vertical-align: top; width: 19px;"><br>
    </td>
    <td style="vertical-align: top; width: 91px;"><br>
    </td>
    <td style="vertical-align: top; width: 100px;"><br> -->
    </td>
    </tr>
    <tr>
    <td style="vertical-align: top; width: 19px;"><?php echo $date; ?><br>
    </td>
    <td style="vertical-align: top; width: 19px;"><?php echo $heure; ?><br>
    </td>
    <td style="vertical-align: top; width: 1px;"><?php echo $user; ?><br>
    </td>
    <td style="vertical-align: top; width: 80px;"><?php echo "". $ordre_x10 ."<br/ >"; ?><br>
    </td>
    <td style="vertical-align: top; width: 19px;"><?php echo $fichier_exec; ?><br>
    </td>
    <td style="vertical-align: top; width: 91px;"><?php echo $macro; ?><br>
    </td>
    <td style="vertical-align: top; width: 100px;"><?php echo $execute_par; ?><br>
    </td>
    </tr>
    </tbody>
    </table>
    Seulement il y a un problème avec les espaces dans le tableau,
    dans "user" il affiche uniquement "root root" au lieu de le mettre a la dessous ligne.
    Dans "ordre X10" il v a à la ligne au lieu d'afficher sur une seule ligne "cm15 E1 On" ou "cm15 E1 bri 15" ??

    Je ne trouve pas mon erreur ??
    Comment dois-je faire ?

    Merci.


    ?>
    Intel I7 960 | 6 Go Ram | 5 HDD au total 3636 Go | Windows 7 Edition intégral x64 | WampServer 2.0c | Apache 2.2.8 | Php 5.2.6 | MySQL 5.0.51b
    DreamPlug | 512 mo ram | SSD 16 GO | Linux debian 2.6.39.4 | armv5tel | Lamp | PHP 5.3.3-7 | Apache 2.2.16 | Mysql 14.14

  2. #2
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2009
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juillet 2009
    Messages : 51
    Points : 44
    Points
    44
    Par défaut
    Pour vérifier le contenu d'un tableau php (array), on utilise la fonction print_r(). Tu pourras ainsi vérifié ce que tu stock dans les tableaux que tu remplis avec explode().

Discussions similaires

  1. Créer tableau dans fichier texte
    Par Hurricae dans le forum Collection et Stream
    Réponses: 5
    Dernier message: 23/05/2011, 12h42
  2. Mettre résultat sur un fichier texte
    Par nathe dans le forum MATLAB
    Réponses: 23
    Dernier message: 09/06/2009, 15h34
  3. Mettre resultat dans un fichier texte
    Par xoooom dans le forum VBScript
    Réponses: 2
    Dernier message: 15/01/2009, 13h47
  4. Réponses: 8
    Dernier message: 23/06/2006, 13h51
  5. tableau et fichier text
    Par tarekcom dans le forum Débuter
    Réponses: 25
    Dernier message: 05/05/2006, 21h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo