Click to See Complete Forum and Search --> : Opening and reading files


deep
08-28-2003, 02:30 AM
Hello!!

I got a file that looks like this(without the Line1, Line2...):

Line1: $MyName;
Line2: $Age;
Line3: $MessageText
Line4: $MessageText
Line5: $MessageText
Line6: $MessageText
...

How to I open and load everything that starts from Line3 to foef() and place that text in a
<textarea name="textfield"></textarea> excactelly as it appears from Line3 to the last line? :confused:

Thx! :)

pyro
08-28-2003, 07:22 AM
Try this out:

<?PHP
$file = file("temp/file.txt");
$text = "";
for ($i=2;$i<count($file);$i++) {
$text .= $file[$i];
}
echo "<textarea rows=\"5\" cols=\"50\" name=\"message\">$text</textarea>";
?>

deep
08-28-2003, 08:09 AM
:) Thats it! Can you just tell me what the .= does? I noticed the . and I havnt seen this before (dont worry im a newbie thats learning) :D

Thanks!

pyro
08-28-2003, 08:19 AM
Sure thing, here's the same script with comments:

<?PHP
$file = file("temp/file.txt"); #open temp/file.txt and put each line into an array named $file;
$text = ""; # set up the variable $text, so we can use it in the for loop
for ($i=2;$i<count($file);$i++) { #set up a loop. It will start at 2 (which is line 3, as arrays start with 0) and run until it reaches the number of lines in the file
$text .= $file[$i]; #set $text to the value of each line after line 3. .= means to concatenat the string with the current value. It is the same as $text = $text.$file[$i]
}
echo "<textarea rows=\"5\" cols=\"50\" name=\"message\">$text</textarea>"; #echo out the text area with the value set to $text
?>Let me know if you have any more questions about it.