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.
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.Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
