W3docs

fgetss()

The fgetss() function is a built-in PHP function that reads a line from a file and removes any HTML or PHP tags from the line. This function is similar to the

What is the fgetss() Function?

Note: The fgetss() function was deprecated in PHP 5.3 and removed in PHP 7.0. It is obsolete and will trigger a fatal error on any modern PHP version.

The fgetss() function was a built-in PHP function that read a line from a file and removed any HTML or PHP tags from the line. This function was similar to the fgets() function, but it also stripped tags from the line it read.

Here's the basic syntax of the fgetss() function:

The PHP syntax of fgetss()

fgetss(file, length, allowable_tags);

Where file is the file pointer, length is the maximum length of the line to be read, and allowable_tags is a string containing a list of tags that should not be stripped from the line. If the allowable_tags parameter is not specified, all HTML and PHP tags will be stripped from the line.

How to Read and Strip Tags in PHP?

Using the fgetss() function was similar to using the fgets() function. Here are the steps to follow:

  1. Open the file using the fopen() function.
  2. Use the fgets() function to read a line from the file, then apply strip_tags() to remove any tags.
  3. Close the file using the fclose() function.

Here's an example code snippet that demonstrates the modern approach to reading a file line by line while removing tags:

How to Use fgets() and strip_tags()?

<?php

$file = fopen("file.txt", "r");

// Read the first line from the file
$line = fgets($file);

// Loop through the file until the end is reached
while (!feof($file)) {
    // Remove HTML/PHP tags and process the line
    echo strip_tags($line);

    // Read the next line
    $line = fgets($file);
}

// Close the file
fclose($file);

In this example, we first open a file named file.txt using the fopen() function. Then we read the first line from the file using the fgets() function and store it in the $line variable. We then loop through the file until the end is reached using the feof() function. Inside the loop, we process the line by removing tags with strip_tags() and print it to the screen using the echo statement. Finally, we read the next line from the file using the fgets() function and store it in the $line variable. Once we reach the end of the file, we close the file using the fclose() function.

Conclusion

The fgetss() function is obsolete and has been removed from PHP since version 7.0. It should not be used in new projects. For modern PHP development, use fgets() combined with strip_tags() to read files line by line while safely removing HTML or PHP tags. We hope this guide has been helpful, and we wish you the best of luck in your PHP endeavors!

Practice

Practice

What does the fgetss() function in PHP do?