Prev | Current Page 354 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Its prototype
follows:
string file_get_contents(string filename [, int use_include_path
[resource context]])
By revising the script from the preceding section to use this function instead of
file(), you get the following code:
// Read the file into a string variable
$userfile= file_get_contents("users.txt");
// Place each line of $userfile into array
$users = explode("\n",$userfile);
// Cycle through the array
foreach ($users as $user) {
// Parse the line, retrieving the name and e-mail address
list($name, $email) = explode(" ", $user);
// Output the formatted name and e-mail address
echo "$name/a>
";
292 CHAPTER 10 ?–  WORKING WITH THE FI LE AND OPERATING SYSTEM
}
?>
The use_include_path and context parameters operate in a manner identical to
those defined in the preceding section.
Reading a CSV File into an Array
The convenient fgetcsv() function parses each line of a file marked up in CSV
format. Its prototype follows:
array fgetcsv(resource handle [, int length [, string delimiter
[, string enclosure]]])
Reading does not stop on a newline; rather, it stops when length characters have
been read. As of PHP 5, omitting length or setting it to 0 will result in an unlimited line
length; however, since this degrades performance it is always a good idea to choose a
number that will certainly surpass the longest line in the file.


Pages:
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366