IMX IMX -3 years ago 106
PHP Question

Read and iterate .txt in PHP

I sense that I am almost there.
Here is a .txt file, which is about 60 Kbytes and full of German words. Every word is on a new line.

I want to iterate through it with this code:


$file = "GermanWords.txt";
$f = fopen($file,"r");

$parts = explode("\n", $f);

foreach ($parts as &$v)
echo $v;


When I execute this code, I get:

The word resource is not in the .txt, I do not know where it comes from.

How can I manage to show up all words in the txt?

Answer Source

No need for fopen just use file_get_contents:

$file = "GermanWords.txt";
$contents = file_get_contents($file);
$lines = explode("\n", $contents); // this is your array of words

foreach($lines as $word) {
    echo $word;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download