snk Posted February 5, 2008 Share Posted February 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89620-solved-lines-in-a-txt-file/ Share on other sites More sharing options...
Moon-Man.net Posted February 5, 2008 Share Posted February 5, 2008 Try this and let me know the result: <?php $handle = @fopen('stathis.txt', "r"); if ($handle) { while (!feof($handle)) { $line[] = fgets($handle, 4096); } fclose($handle); } print_r($line); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89620-solved-lines-in-a-txt-file/#findComment-459190 Share on other sites More sharing options...
snk Posted February 5, 2008 Author Share Posted February 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89620-solved-lines-in-a-txt-file/#findComment-459192 Share on other sites More sharing options...
laffin Posted February 5, 2008 Share Posted February 5, 2008 look closely for ($i = 0; $i <= sizeof($line); $i++){ echo $line[i]; shud be for ($i = 0; $i <= sizeof($line); $i++){ echo $line[$i]; Quote Link to comment https://forums.phpfreaks.com/topic/89620-solved-lines-in-a-txt-file/#findComment-459202 Share on other sites More sharing options...
Moon-Man.net Posted February 5, 2008 Share Posted February 5, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/89620-solved-lines-in-a-txt-file/#findComment-459205 Share on other sites More sharing options...
snk Posted February 5, 2008 Author Share Posted February 5, 2008 it works like a charm.. thank you very much. the funny characters caused because my php uses utf8 and i saved the txt as Unicode. I use a non English language inside. Thank you again, you were very... "explainful" Quote Link to comment https://forums.phpfreaks.com/topic/89620-solved-lines-in-a-txt-file/#findComment-459227 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.