PHP fgetss() Function: Read a Line from a File and Strip HTML/PHP Tags

This article explains the PHP fgetss() function, its signature, parameter details, return behavior, and provides a complete example that reads a file while removing HTML and PHP tags, demonstrating the output and usage nuances.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP fgetss() Function: Read a Line from a File and Strip HTML/PHP Tags

The fgetss() function reads a line from a file pointer and automatically strips out any HTML and PHP tags, behaving similarly to fgets() but with tag removal.

Signature:

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

Parameters:

handle : a valid file pointer opened by fopen() or fsockopen() and not yet closed.

length (optional): the maximum number of bytes to read.

allowable_tags (optional): a string of tags that should not be stripped.

Return value: the read string with HTML/PHP tags removed, or FALSE on error.

Example usage:

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

Output produced by the script:

Welcome! Today is the  of .
Text outside of the HTML block.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPfile-handlingfgetssHTML stripping
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.