PHP Tutorial - PHP fgetss() Function






Definition

The fgetss() function returns a line, with HTML and PHP tags removed, from an open file.

Syntax

PHP fgetss() Function has the following syntax.

fgetss(file,length,tags)

Parameter

ParameterIs RequiredDescription
fileRequired.File to check
lengthOptional.Number of bytes to read. Default is 1024 bytes.
tagsOptional.Tags that will not be removed

Return

This function returns FALSE on failure.





Example


<?php/*from w ww .  j  a  va  2s  .co  m*/
$str = <<<EOD
<html><body>
 <p>Welcome!.</p>
</body></html>
Text outside of the HTML block.
EOD;
file_put_contents('index.php', $str);

$handle = @fopen("index.php", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgetss($handle, 1024);
        echo $buffer;
    }
    fclose($handle);
}
?>

The code above generates the following result.





Example 2

The following code shows how to get 1024 character from a file and keep paragraph and bold tag.




<?php
    $file = fopen("test.txt","r");
    echo fgetss($file,1024,"<p>,<b>");
    fclose($file);
?>

The code above generates the following result.

Example 3

The following code shows how to get a line, with HTML and PHP tags removed, from an open file.


<?php
    $file = fopen("a.php","r");
    echo fgetss($file);
    fclose($file);
?>