Foros del Web » Programando para Internet » PHP »

crear array de txt

Estas en el tema de crear array de txt en el foro de PHP en Foros del Web. hola amigos quisiera preguntarles si se puede crear un array desde un txt, es decir : archivo.txt: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código txt: Ver original 1 2 ...
  #1 (permalink)  
Antiguo 29/07/2010, 05:36
Avatar de danistrein  
Fecha de Ingreso: septiembre-2008
Ubicación: Salta Capital
Mensajes: 550
Antigüedad: 15 años, 7 meses
Puntos: 9
crear array de txt

hola amigos quisiera preguntarles si se puede crear un array desde un txt, es decir :
archivo.txt:
Código txt:
Ver original
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
como podria hacer para que lo que esta dentro del txt se me forme asi?
Código PHP:
Ver original
  1. $numeros=array("1","2","3","4","5");
alguien me podria decir,
__________________
Appones.net - www.appones.net
  #2 (permalink)  
Antiguo 29/07/2010, 06:04
Avatar de Chenon  
Fecha de Ingreso: abril-2005
Ubicación: Palma de Mallorca
Mensajes: 99
Antigüedad: 19 años
Puntos: 11
Respuesta: crear array de txt

Es muy fácil.

Código PHP:
Ver original
  1. <?php
  2. $tal = array();
  3. $file = fopen("welcome.txt", "r") or exit("Unable to open file!");
  4. //Output a line of the file until the end is reached
  5. while(!feof($file)) {
  6.   array_push($tal,fgets($file));
  7. }
  8. fclose($file);
  9.  
  10. print_r($tal);
  11. ?>

Sacado de: http://www.w3schools.com/PHP/php_file.asp

Lo que haces simplemente es abrir un archivo, y lo recorres linea por linea, añadiendo cada una de esas lineas a un array existente.

No he probado si funciona, pero creo haberlo probado antes y me fue bien.
  #3 (permalink)  
Antiguo 29/07/2010, 06:15
Avatar de danistrein  
Fecha de Ingreso: septiembre-2008
Ubicación: Salta Capital
Mensajes: 550
Antigüedad: 15 años, 7 meses
Puntos: 9
Respuesta: crear array de txt

muchas gracias Chenon, me ah funcionado muy bien ^^.
__________________
Appones.net - www.appones.net
  #4 (permalink)  
Antiguo 29/07/2010, 09:22
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: crear array de txt

O mucho más rápido puedes usar file().

Saludos.
  #5 (permalink)  
Antiguo 29/07/2010, 09:27
Colaborador
 
Fecha de Ingreso: octubre-2009
Ubicación: Tokyo - Japan !
Mensajes: 3.867
Antigüedad: 14 años, 6 meses
Puntos: 334
Respuesta: crear array de txt

Código PHP:
Ver original
  1. print_r(file("welcome.txt"));

edito.. ya lo habia puesto GatorV :(
__________________
More about me...
~ @rhyudek1
~ Github

Etiquetas: 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 15:19.