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
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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.