tags:

views:

100

answers:

5

I have a text file with spam words. I want to have an array filled with those words. I've tried doing:

$fp = @fopen("../files/spam.txt",'rb');
$words = fgetcsv($fp,100,"\n");

but it doesn't work (words only has the first letter of the txt file in it first cell).

do you how to do this?

EDIT: the .txt file looks like this:

yahoo
google
msn
blah
blah

EDIT: I DONT KNOW WHAT IS A CSV FILE! THIS IS A TEXT FILE! I JUST GIVE AN EXAMPLE.

please could some1 help me it looks really easy, i just dont understand.

A: 

How about splitting the string you read from the file into an array?

split() function definition.

$string = "Niagara Becks Corn";
$array = split(" ", $string);
# ["Niagara", "Becks", "Corn"]
The MYYN
you can also use explode function
yoda
A: 

Use the 'file' function to read a PHP file from the disk into an array. See the manual here: http://php.net/manual/en/function.file.php

TheGrandWazoo
not if it's in csv format
Galen
Doesn't look like csv to me.
TheGrandWazoo
OP doesn't know what a csv is apparently =)
Galen
+1  A: 

All you need to do is:

$words = file('./files/spam.txt',FILE_IGNORE_NEW_LINES);
Arthur Frankel
+1  A: 

That is not a CSV file. CSV stands for comma separated values. You have no commas!

$spam_words = file('../files/spam.txt', FILE_IGNORE_NEW_LINES);
Galen
thanks, i just didn't get which one i should use, besides everyone here recommends something else it's really confusing "CSV! CSV! it's CSV!" - god ppl can't you see i'm a newbie??
sombe
you told us it was a csv =)
Galen
A: 

Try this

$file = file_get_contents('spam.txt');
$file_words = explode(" ", $file);
$file_count = count($file_words);

for ($i=0; $i < $file_count; $i++){
    echo $file_words[$i] . "<br />";
}

Your spam.txt file should look like this:

yahoo msn google