Foros del Web » Programando para Internet » PHP »

leer caracteres de un txt y guardarlo en un array

Estas en el tema de leer caracteres de un txt y guardarlo en un array en el foro de PHP en Foros del Web. hola, hice un programa que lee y muestra un pedazo de un texto en especifico linea por linea. Este es un extracto del texto Código: ...
  #1 (permalink)  
Antiguo 14/01/2010, 05:50
 
Fecha de Ingreso: enero-2010
Mensajes: 62
Antigüedad: 14 años, 3 meses
Puntos: 1
leer caracteres de un txt y guardarlo en un array

hola, hice un programa que lee y muestra un pedazo de un texto en especifico linea por linea.

Este es un extracto del texto
Código:
1NGN1LB        0000 016 142 3 51544590         0                              1 09/12/01 01:19:43 00006  1 4246798                        0                      
1       ENT1PB 1047 016 172 3 24426555         3 24426555                     1 09/12/01 01:19:39 00010  1 123019163315772                0                      
1PCS4CB PCS1XO 0017 016 142 3 984606404        0                              1 09/12/01 01:19:03 00047  1 4425992                        0                      
1NGN1LB        0000 016 142 3 43410165         0                              1 09/12/01 01:19:43 00007  1 4246797                        0                      
1              0000 016 172 3 24608106         3 24608106                     1 09/12/01 01:19:36 00014  1 4200742                        0                      
1SAT1CB OPX2PB 0249 016 142 3 991742319        0                              1 09/12/01 01:18:15 00095  1 412141457                      0                      
1       ENT1PB 1043 016 172 3 24426555         3 24426555                     1 09/12/01 01:18:57 00054  1 123019513519188                0                      
1VTR1LB NGN1LB 0029 016 142 3 27242421         0                              1 09/12/01 01:15:50 00241  1 3696042                        0                      
1PP61LB        0000 016 142 3 23236879         0                              1 09/12/01 01:19:44 00007  1 4246899                        0                      
1NGN1LB        0000 016 142 3 27255003         0                              1 09/12/01 01:19:46 00006  1 4246797                        0                      
1VTR1LB NGN1LB 0016 016 142 3 27255003         0                              1 09/12/01 01:19:46 00006  1 6003612222                     0                      
1PCS4CB PCS1XO 0021 016 142 3 987469262        0                              1 09/12/01 01:19:07 00045  1 4425992                        0                      
1       ENT1PB 1041 016 172 3 24426555         3 24426555                     1 09/12/01 01:19:47 00006  1 123019163312026                0

y usando substr() logré separar los datos específicos de cada línea.

Código PHP:
 <html>

<head>
  <title></title>
</head>


<?php
$handle 
= @fopen("texto\en-2-2.txt""r");
if (
$handle) {  ?>
<div align=center>En - 2</div>
    <TABLE style="border: 2px dotted gray;margin-right:auto;margin-left:auto;">
    <tr style="text-align:center;background-color:#4a6890;color:#fff;">
    <td width="61">TgrpA</td>
    <td width="61">TgrpB</td>
    <td width="61">Nadi142</td>
    <td width="61">Fecha</td>
    <td width="61">Hora</td>
    <td width="61">Durac</td>
    <td width="61">NadiB</td>
    <td width="61">number</td>

  </tr>
    <?


    
while (!feof($handle)) {
        
$buffer fgets($handle);
          
?>



                <? $TgrpA substr("$buffer",16);

                   
$TgrpB substr("$buffer",77);

                   
$Nadi142 substr("$buffer",309);

                   
$Fecha substr("$buffer",808);

                   
$Hora substr("$buffer",898);

                   
$Durac substr("$buffer",985);

                   
$NadiB substr("$buffer",10715);

                   
$number substr("$buffer"1073); ?>



<TR>

                <TD><? echo $TgrpA "\n";?></TD>

                <TD><? echo $TgrpB "\n"?></TD>

                <TD><? echo $Nadi142 "\n"?></TD>

                <TD><? echo $Fecha "\n"?></TD>

                <TD><? echo $Hora "\n"?></TD>

                <TD><? echo $Durac "\n"?></TD>

                <TD><? echo $NadiB "\n"?></TD>

                <TD><? echo $number "\n"?></TD>

                 </TR>
           <?



              
}



    
fclose($handle);  ?>
     </TABLE>

         <?

     ?>
                       <? echo '<br>'?>



                <?





}


?>


</body>

</html>
lo que necesito hacer es comparar los datos de la variable $number y me diga cuantas coincidencias hay en 1 dato y sume la duracion, me explico

tengo eso
$number $Durac
1 20
5 14
2 2
5 3


quiero que me diga esto

1 = > se repite 1 vez y dura 20
5 = > se repite 2 veces y dura 17
2 = > se repite 1 vez 2


ojalá me puedan ayudar
  #2 (permalink)  
Antiguo 14/01/2010, 06:01
Avatar de CHuLoSoY  
Fecha de Ingreso: febrero-2002
Ubicación: Ribeira (Galicia)
Mensajes: 1.900
Antigüedad: 22 años, 2 meses
Puntos: 29
Respuesta: leer caracteres de un txt y guardarlo en un array

Yo empezaría por guardar todo en un array y luego mirar las coincidencias cuando tengas eso listo.

Código PHP:
Ver original
  1. /* Ejemplo */
  2.  
  3. $abrir=fopen($archivo, "r");
  4.  
  5. if ($abrir) {
  6.  
  7.     $datos=file($archivo);
  8.  
  9.     $NumFilas=count($datos);
  10.  
  11.     for($n=0;$n<$NumFilas;$n++) {
  12.         $linea=explode(" ", $datos[$n]);
  13.                 $TgrpA=datos[0];
  14.                 $TgrpB=datos[1];
  15.                 $Nadi142=datos[2];
  16.                 $fecha=datos[3];
  17.  
  18.                   /*Etc etc*/
  19.  
  20.     }
  21. }

Saludos.
__________________
ESQUIO Dominios y Hosting
Las mejores características con los mejores precios.
  #3 (permalink)  
Antiguo 14/01/2010, 12:59
 
Fecha de Ingreso: enero-2010
Mensajes: 62
Antigüedad: 14 años, 3 meses
Puntos: 1
Respuesta: leer caracteres de un txt y guardarlo en un array

lo que me pasaste no lo entendí muy bien, pero ya resolví el problema sobre como crear el array

primero creé un array() vacio y despues le fuí agregando los datos con array_push() en el while.

dando como resultado esto al imprimir el array
Código:
 Array ( [0] => 424 [1] => 123 [2] => 442 [3] => 424 [4] => 420 [5] => 412 [6] => 123 [7] => 369 [8] => 424 [9] => 424 [10] => 600 [11] => 442 [12] => 123 )

Código PHP:

<html>

<head>
  <title></title>
</head>


<?php

$texto 
=  "texto\en-2-2.txt";
$handle = @fopen($texto"r");
$array_number = array();

if (
$handle) {  ?>
<div align=center>En - 2</div>
    <TABLE style="border: 2px dotted gray;margin-right:auto;margin-left:auto;">
    <tr style="text-align:center;background-color:#4a6890;color:#fff;">
    <td width="61">number</td></tr>
    <?
    
while (!feof($handle)) {
        
$buffer fgets($handle);
          
?>
              <? $number substr("$buffer"1073); ?>
<TR><TD><? echo $number "\n"?></TD></TR>


<? array_push($array_number $number);
}
    
print_r (array_values($array_number));
    
fclose($handle);  ?>
     </TABLE>
         <?
     ?>
                       <? echo '<br>'?>
                <?
}
?>
</body>
</html>

Etiquetas: caracteres, txt
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:38.