fgets — Read a line from the file pointer
Description
string fgets ( resource $handle [, int $length ] )
Read a line from the file pointer.
Parameters
handle The file pointer must be valid and must point to a file successfully opened by fopen() or fsockopen() (and has not been closed by fclose()).
length Reads a line from the file pointed to by handle and returns a string with a length of at most length - 1 byte. Stops when a newline character (included in the return value), EOF, or length - 1 bytes has been read (whichever occurs first). If length is not specified, it defaults to 1K, or 1024 bytes.
Note:
Starting from PHP 4.3, if length is omitted, the length of the line is assumed to be 1024, and data will continue to be read from the stream until the end of the line. If most of the lines in the file are larger than 8KB, specifying the maximum line length in the script is more efficient in utilizing resources.
Return value
Returns a string after reading length - 1 bytes from the file pointed to by the pointer handle. Returns FALSE if there is no more data in the file pointer.
Returns FALSE when an error occurs.
fgets() function example, read the file line by line , the code is as follows
<?php $handle = @ fopen ( "/tmp/inputfile.txt" , "r" ); if ( $handle ) { while (( $buffer = fgets ( $handle , 4096 )) !== false ) { echo $buffer ; } if (! feof ( $handle )) { echo "Error: unexpected fgets() fail\n" ; } fclose ( $handle ); } ?>
getss - Read a line from the file pointer and filter out HTML tags
Descriptionstring fgetss ( resource $handle [, int $length [, string $allowable_tags ]] )
The file pointer must be valid and must point to a file successfully opened by fopen() or fsockopen() (and has not been closed by fclose()).
lengthRetrieve the data of this length.
allowable_tagsYou can use the optional third parameter to specify which tags are not to be removed.
Return valueRead length - 1 byte characters from the file pointed to by handle, and filter out all HTML and PHP code. getss () function example, read a PHP file line by line, the code is as follows<?php $str = <<<EOD <html><body> <p>Welcome! Today is the <?php echo(date('jS')); ?> of <?= date('F'); ?>.</p> </body></html> Text outside of the HTML block. EOD; file_put_contents ( 'sample.php' , $str ); $handle = @ fopen ( "sample.php" , "r" ); if ( $handle ) { while (! feof ( $handle )) { $buffer = fgetss ( $handle , 4096 ); echo $buffer ; } fclose ( $handle ); } ?>
Welcome! Today is the of .Text outside of the HTML block.
The above is the detailed content of Instructions for using php fgets() function and fgetss() function. For more information, please follow other related articles on the PHP Chinese website!