Page 1 of 1
Text File --> Array
Posted: Tue Jun 06, 2006 9:41 pm
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!
Posted: Tue Jun 06, 2006 9:43 pm
by feyd
not enough research...
file()
Posted: Tue Jun 06, 2006 9:59 pm
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()
Posted: Tue Jun 06, 2006 10:03 pm
by Christopher
You really need to spend a little time looking through the Array, Filesystem and String library sections of the PHP manual.
Posted: Tue Jun 06, 2006 10:10 pm
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;
?>
Posted: Wed Jun 07, 2006 12:04 am
by n00b Saibot
Posted: Wed Jun 07, 2006 12:17 am
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.
Posted: Wed Jun 07, 2006 12:19 am
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

Posted: Wed Jun 07, 2006 6:34 am
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 />");
}
Posted: Wed Jun 07, 2006 7:59 am
by Grim...
Because handing over the answer isn't going to help anyone learn, is it?
Posted: Wed Jun 07, 2006 12:00 pm
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;
?>
Posted: Wed Jun 07, 2006 12:01 pm
by tecktalkcm0391
nevermind I think i got it!