Q:

Write a php program to differentiate between fgets, fgetss and fgetcsv

belongs to collection: PHP Programming Exercises

0

Write a php program to differentiate between fgets, fgetss and fgetcsv

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

fgets() function

fgets() function reads one line at a time from a file. It reads until it encounters a new line character (\n) or EOF. The maximum length read is the length specified minus 1 byte.

You can use many different functions to read from files. The fgets() function, for example, is useful when you are dealing with a files that contain plain text that you want to deal with in chunks.

string fgets ( resource $handle [, int $length ] )

Example

<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>

fgetss() function

An interesting variation on fgets() is fgetss(), which has the following syntax -

string fgetss(resource fp, int length, string [allowble_tags])

The function is similar to fgets() except that it strips out any PHP and HTML tags found in string. If you want to leave in any particular tags, you can add them in the allowable_tags string. You would use fgetss() for safety when reading a file written by someone else or containing user input.

Example

<?php
$file = fopen("test.txt","r");
echo fgetss($file);
fclose($file);
?>

fgetcsv() function

The function fgetcsv() is another variation on fgets(). It has the following prototype.

array fgetcsv(resource fp, int length [, string  delimiter [, string enclosure]])

The function breaks up lines of files when you have used a delimiting character, such as the tab character or a comma. If you want to reconstruct the variable from the order separately rather than as a line of text, fgetcsv() allows you to do this simply.

Example

<?php
$file = fopen("test.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Programming Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
There are two deals of an item to buy. The quantit... >>
<< Write a program to loop through an associative arr...