PHP Function Reference

PHP fgetss() Function



The PHP fgetss() function is identical to fgets() function, except this function attempts to strip any NUL bytes, HTML and PHP tags from the text it reads.

The function retains the parsing state from call to call. Therefore it is not equivalent to calling strip_tags() on the return value of fgets() function.

Note: This function has been DEPRECATED as of PHP 7.3.0, and REMOVED as of PHP 8.0.0.

Syntax

fgetss(stream, length, allowable_tags)

Parameters

stream Required. Specify the file pointer to return a line from. It must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).
length Optional. Specify maximum number of bytes to be read from a line. Reading ends when length - 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first). If not specified, it will keep reading from the stream until it reaches the end of the line.
allowable_tags Optional. Specify tags which should not be stripped.

Return Value

Returns a string of up to length - 1 bytes read from the file pointed to by stream, with all HTML and PHP code stripped. If there is no more data to read in the file pointer, it returns false. If an error occurs, it returns false.

Example:

In the example below, the file main.php is read line by line using this function which results into all HTML and PHP code stripped from the read line.

<?php
$str = <<<EOD
<html><body>
<p>Test paragraph.</p><!-- Comments -->
<?php echo "Hello World."; ?>
<a href="#testlink">Some link</a>
</body></html>
Text outside the HTML block.
EOD;

//writing $str to main.php file
file_put_contents('main.php', $str);

//open main.php in read mode
$fp = fopen("main.php", "r");

//reading the file line by line with 
//all HTML and PHP code stripped and 
//displaying the read content
while(!feof($fp)) {
  $buffer = fgetss($fp, 4096);
  echo $buffer;
}

//close the file
fclose($fp);
?>

The output of the above code will be:

Test paragraph.

Some link

Text outside the HTML block.

❮ PHP Filesystem Reference