PHP fgetss() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fgetss() used to read one line from a file and strip the HTML and PHP tags from it. The function was deprecated in PHP 7.3 and removed completely in PHP 8.0.

If you still see fgetss() in old code, it will crash on PHP 8 with an undefined function error. The replacement is easy: read the line with fgets() and clean it with strip_tags().

Syntax

Call fgetss() with the values you need. Start with this line.

PHP
$line = strip_tags(fgets($handle));
CallWhat it does
strip_tags(fgets($handle))The modern replacement for fgetss($handle). Reads a line and removes all tags.
strip_tags(fgets($handle), '<b>')Replaces fgetss($handle, null, '<b>'). Keeps the b tags and removes the rest.
function_exists('fgetss')Returns false on PHP 8, which tells you the function is gone.

fgetss() was deprecated in PHP 7.3 and removed in PHP 8.0. Do not use it in new code. Replace every call with strip_tags(fgets($handle)). The $allowable_tags argument maps to the second argument of strip_tags().

Library declarations
PHP
fgetss(resource $stream, ?int $length = null, string $allowable_tags = ?): string|false

Parameters

ParameterDescription
streamRequired (old versions only). An open file stream, such as the one returned by fopen().
lengthOptional (old versions only). The number of bytes to read, as in fgets().
allowable_tagsOptional (old versions only). Tags that should not be stripped. In strip_tags() this is the second argument.

Return Value

Return ValueDescription
stringIn PHP 7 and earlier, the line with HTML and PHP tags removed.
falseIn PHP 7 and earlier, returned at the end of the file or on failure. In PHP 8 the function no longer exists.

PHP Examples

Example 1 — Migrate to strip_tags(fgets())

Read a line with fgets() and remove its tags with strip_tags(). This does what fgetss() used to do.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetss.txt';
file_put_contents($path, '<b>Bold</b> <i>text</i>' . PHP_EOL);

$handle = fopen($path, 'r');
$line = fgets($handle);

if ($line !== false) {
    echo trim(strip_tags($line)), PHP_EOL;
}

fclose($handle);
unlink($path);

How It Works

  1. The file contains one line with b and i HTML tags.
  2. fgets() reads the line with its tags still in place.
  3. strip_tags() removes every tag and keeps only the text between them.
  4. The if check makes sure fgets() did not return false, and trim() removes the line break.

Example 2 — The Function Is Gone in PHP 8

On PHP 8 and later, calling fgetss() throws an Error. Catch it to see the message, or use function_exists() to check first.

PHP
<?php
echo 'fgetss exists: ', var_export(function_exists('fgetss'), true), PHP_EOL;

try {
    $line = fgetss(STDIN);
} catch (Error $e) {
    echo $e->getMessage(), PHP_EOL;
}

How It Works

  1. function_exists() returns false because PHP 8 removed fgetss().
  2. Calling the missing function throws an Error object.
  3. Error is the right class to catch, because this is not an Exception.
  4. getMessage() prints the text of the error so you can recognize it in your logs.

Example 3 — Keep Some Tags

The old $allowable_tags argument becomes the second argument of strip_tags(). Here the bold tags are kept and all other tags are removed.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetss_keep.txt';
file_put_contents($path, '<b>Bold</b> <i>text</i>' . PHP_EOL);

$handle = fopen($path, 'r');
$line = fgets($handle);

echo trim(strip_tags($line, '<b>')), PHP_EOL;

fclose($handle);
unlink($path);

How It Works

  1. fgets() reads the line that contains both b and i tags.
  2. strip_tags() receives '<b>' as the list of tags to keep.
  3. The i tags are removed, but the b tags stay in the result.
  4. This replaces the old call fgetss($handle, null, '<b>').

Frequently Asked Questions

Yes. fgetss() was deprecated in PHP 7.3 and removed in PHP 8.0. Calling it on PHP 8 throws an Error saying the function is undefined.
Use strip_tags(fgets($handle)). fgets() reads the line and strip_tags() removes the HTML and PHP tags from it.
Pass the allowed tags as the second argument of strip_tags(), for example strip_tags($line, '<b><i>').
Your server was upgraded to PHP 8 or later, where the function no longer exists. Replace each fgetss() call with strip_tags(fgets()).
No. strip_tags() removes tags but it is not a full security filter. When you print user text in HTML, also use htmlspecialchars().

Next: file()

file() reads a whole file and returns it as an array, with one item for each line. It is t.

file() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful