Page 1 of 1
file(), file_get_contents(), or something else?
Posted: Fri Feb 20, 2009 1:18 pm
by oboedrew
Hello, everyone. I've got a PHP question. I'm trying to find a way to open a file and create an array from it, as if using file(). However, I'd like the first line to be file[0], the second to be file[1], and then starting on the third line I'd like to read the rest of the file as a string, as if using file_get_contents(), and make that entire string file[2], even though that string may take up a number of lines in the text file. Is this possible?
Thanks,
Drew
Re: file(), file_get_contents(), or something else?
Posted: Fri Feb 20, 2009 1:49 pm
by pickle
Do you want $file[2] to consist of the entire file, or just lines from the third line on?
Re: file(), file_get_contents(), or something else?
Posted: Fri Feb 20, 2009 2:04 pm
by oboedrew
$file[2] should contain everything from the third line on. So, basically, I'm trying to read the first two lines with file(), and then read the rest of the lines with file_get_contents().
Cheers,
Drew
Re: file(), file_get_contents(), or something else?
Posted: Fri Feb 20, 2009 2:14 pm
by pickle
- Open the file with file()
- array_shift() the first element off & store it in a new array
- array_shift() the first element off again & store it in the new array (this would be the second line of the file)
- call implode() on the original array, and store the result in the [2] element of the new array
Re: file(), file_get_contents(), or something else?
Posted: Sat Feb 21, 2009 9:49 am
by oboedrew
Ooh, very clever, pickle. I'll try that out. I had never even heard of the array_shift() function.
Thanks,
Drew