Jump to content

[SOLVED] lines in a .txt file


snk

Recommended Posts

Hello,

 

I would like to know how can I read lines from a txt file? I use the below code but i get a blank page.

<?php
$handle = @fopen('stathis.txt', "r");
if ($handle) {
   while (!feof($handle)) {
       $line[] = fgets($handle, 4096);
   }
   fclose($handle);
}
for ($i = 0; $i <= sizeof($line); $i++){
echo $line[i];
} //for
?>

 

when i echo sizeof($line); I get 1776 which is the number of lines in my file, but why i dont get any results? any help?

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/89620-solved-lines-in-a-txt-file/
Share on other sites

thank you for the reply, the result is

 

Array ( [0] => ��a�1�2�3� ������� ���������� �-� ������������ � � [1] => �a�1�7� �������� ��������� �-� ������ � � [2] => �a�_�e�v�a�g�e�l� ��������������� ���������� �-� ������������ � � [3] => .......

 

going to the end.

print_r($array) prints out an array, from start to finish. Now what is displayed on your page, is funnily enough, the content of $line.

 

OK, providing "��a�1�2�3� ������� ���������� �-� ������������ � � " is the first line, and "�a�1�7� �������� ��������� �-� ������ � � " the second and so on so forth, this is good.

 

Have a look at the foreach statement, it is a much better way of looping through array's...

http://au.php.net/manual/en/control-structures.foreach.php

 

You should replace your `for` statement with this:

 

foreach ($line as $single_line) {
    echo $single_line ."<br>\n";
}

 

You should then get what ever is in stathis.txt printed to your page....

 

Does stathis.txt actually contain all that crap?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.