Text File --> Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Text File --> Array

Post by tecktalkcm0391 »

How would you get the contents of a text file and then make it into an array?

I've done some research and couldn't find a thing!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not enough research... :P

file()
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

i just found something and i was going to post this:

Code: Select all

<?php 
// file example 1: read a text file into an array, with 
// each line in a new element 

$filename="input.txt"; 
$lines = array(); 
$file = fopen($filename, "r"); 
while(!feof($file)) { 

    //read file line by line into a new array element 
    $lines[] = fgets($file, 4096); 
   
} 
fclose ($file); 
print_r($lines); 
?>
and see if its right, but i think i will go with File()
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You really need to spend a little time looking through the Array, Filesystem and String library sections of the PHP manual.
(#10850)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Ok.. will do, i really don't have a lot of time. I am only 15, and I have a life like everyone eles here.

But, I have a question: How come I get 'array' when I echo the array $badwords?

Code: Select all

<?php
$lines = file('input.txt');
$badwords = array();
foreach ($lines as $line_num => $line) {
   $badwords[] = "$line";
}

echo $badwords;

?>
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

print_r() is for you
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

tecktalkcm0391 wrote:Ok.. will do, i really don't have a lot of time. I am only 15, and I have a life like everyone eles here.
It would probably take about 15-30 minutes. I too have a life and I seem to find time to check the manual several times a day.
tecktalkcm0391 wrote:But, I have a question: How come I get 'array' when I echo the array $badwords?
Because it is an array. echo only prints strings. Add the "Variables handling" and "Misc." library sections of the manual to your list.
(#10850)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

arborint wrote:
tecktalkcm0391 wrote:Ok.. will do, i really don't have a lot of time. I am only 15, and I have a life like everyone eles here.
It would probably take about 15-30 minutes. I too have a life and I seem to find time to check the manual several times a day.
downloading the manual makes it very handy ;)
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Post by Li0rE »

Im surprised no one said that you could just do

Code: Select all

$file = file('yourfile');
for($i=0; $i<count($file); $i++)
{
     echo("Line $i: $file[$i] <br />");
}
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Because handing over the answer isn't going to help anyone learn, is it?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

arborint wrote:tecktalkcm0391 wrote:
Ok.. will do, i really don't have a lot of time. I am only 15, and I have a life like everyone eles here.

It would probably take about 15-30 minutes. I too have a life and I seem to find time to check the manual several times a day.

tecktalkcm0391 wrote:
But, I have a question: How come I get 'array' when I echo the array $badwords?

Because it is an array. echo only prints strings. Add the "Variables handling" and "Misc." library sections of the manual to your list.
Ok, I've looked at them, but I didn't see anything that can help me with what I am trying to do. I was asking about the printing the array, but I wasn't really clear, I really want to figure out how to get the contents of a text file and then make it into an array.

This what I have:

Code: Select all

<?php 
$lines = file('input.txt'); 
$badwords = array(); 
foreach ($lines as $line_num => $line) { 
   $badwords[] = "$line"; 
} 

print_r $badwords; 

?>
Last edited by tecktalkcm0391 on Wed Jun 07, 2006 6:29 pm, edited 2 times in total.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

nevermind I think i got it!
Post Reply