Home > Backend Development > PHP Tutorial > Instructions for using php fgets() function and fgetss() function

Instructions for using php fgets() function and fgetss() function

怪我咯
Release: 2023-03-13 13:48:01
Original
1965 people have browsed it

fgets — Read a line from the file pointer

Description

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

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 );
}
?>
Copy after login

getss - Read a line from the file pointer and filter out HTML tags

Description

string fgetss ( resource $handle [, int $length [, string $allowable_tags ]] )
Copy after login

Same as fgets(), except that fgetss() attempts to strip any HTML and PHP markup from the text it reads.

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

Retrieve the data of this length.

allowable_tags

You can use the optional third parameter to specify which tags are not to be removed.

Return value

Read 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(&#39;jS&#39;)); ?> of <?= date(&#39;F&#39;); ?>.</p>
</body></html>
Text outside of the HTML block.
EOD;
file_put_contents ( &#39;sample.php&#39; ,  $str );

$handle  = @ fopen ( "sample.php" ,  "r" );
if ( $handle ) {
    while (! feof ( $handle )) {
         $buffer  =  fgetss ( $handle ,  4096 );
        echo  $buffer ;
    }
     fclose ( $handle );
}
?>
Copy after login

Output result:

Welcome! Today is the  of .Text outside of the HTML block.
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template