homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Reading a text file into an array
Scooter24




msg:1299352
 9:04 pm on Sep 22, 2002 (gmt 0)

I'm trying to read a text file (134 KB, 3500 lines) into an array using this code:

$fp = fopen("myfile.txt", "r");
$temp = fread ($fp, filesize ("myfile.txt"));
fclose ($fp);
$datalines = explode ("\n", $temp);

However the elements of the $datalines array are empty, i.e. they do not contain the lines of the file.
Before doing this I tried:

$datalines = file ("myfile.txt");

getting an array with 3500 empty elements. What can I do? Needless to say, I'm quite frustrated. In any major programming language you can declare an array of a predefined size, it exists and it is able to store what it has to store.

I might add that in a second step I want to sort the lines of this array using a sort function (will probably use a simple bubblesort). So I really need the array data structure otherwise the sorting gets quite difficult.

By the way, my provider has PHP 4.2.

 

jatar_k




msg:1299353
 9:29 pm on Sep 22, 2002 (gmt 0)

>>In any major programming language you can declare an array of a predefined size

not if you mess it up ;)

Have you verified the data to ensure that everything is OK with text file? When you wrote these random lines of text to the txt did you write a newline char to the end of each line? It seems to know how many lines are there but doesn't get the data into it.

Have you tried a preg_split instead to see if that works?

Scooter24




msg:1299354
 11:05 pm on Sep 22, 2002 (gmt 0)

The file contains newlines, so that shouldn't be the problem. I'll tr out the pregsplit and see if it works.

Is there any way to declare an array of a predefined size in PHP?

Scooter24




msg:1299355
 3:00 am on Sep 23, 2002 (gmt 0)

preg_split didn't work, but I tried this:

$datalines = file ("myfile.txt");

foreach ($datalines as $zz) {
echo $zz . "<br>"; }

for ($i = 0; $i <= 10; $i++) {
echo $i . ": " . $datalines[i] . "<br>"; }

The foreach thing correctly echoed all lines of the file, but the for loop just echoed empty lines.

It seems to be an indexing or key problem. What key do I need to use to access the elements of the array? An integer from 0 to number of elements-1 or what else?

jatar_k




msg:1299356
 3:31 am on Sep 23, 2002 (gmt 0)

>>echo $i . ": " . $datalines[i] . "<br>"; }

should be

echo $i . ": " . $datalines[$i] . "<br>"; }

I hope that's the problem because there is no reason why it shouldn't work.

Scooter24




msg:1299357
 9:36 am on Sep 23, 2002 (gmt 0)

Thanks, that fixed the problem. I can't believe I lost 24 hours on such a simple error.

Since something like "$datalines[i]" is always incorrect, why doesn't the PHP parser generate an error report?

andreasfriedrich




msg:1299358
 12:03 pm on Sep 23, 2002 (gmt 0)

Since something like "$datalines[i]" is always incorrect, why doesn't the PHP parser generate an error report?

It will issue a notice if you enable error reporting with

error_reporting(E_ALL);

SpY_MoBiLe




msg:1299359
 6:36 am on Oct 3, 2002 (gmt 0)

I'm trying to do a similar thing: open a text file into a <textarea> tag so you can modify the contents and then save it overwriting the old text file. now i know how to save the contents once it has been placed in the <textarea>, but I'm still not sure on what code is used to insert the contents of the text file into the page. Thank you.

jatar_k




msg:1299360
 4:40 pm on Oct 3, 2002 (gmt 0)

Welcome to WebmasterWorld [webmasterworld.com] SpY_MoBiLe

All you have to do is use fopen [php.net] to open the text file, use fread to read the contents into a var and then echo the var between the textarea tags. Then do what you need to when it's submitted.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved