fgetss() function in PHP


The fgestss() function gets a line from the file pointer and strip HTML and PHP tags. The fgetss() function returns a string of up to length - 1 bytes read from the file pointed to by handle, with all HTML and PHP code striped. If an error occurs, returns FALSE.

Syntax

fgetss(file_path,length,tags)

Parameters

  • file_pointer − The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).

  • length − Length of the data

  • tags − The tags that you don’t want to remove.

Return

The fgetss() function returns a string of up to length - 1 bytes read from the file pointed to by handle, with all HTML and PHP code striped. If an error occurs, returns FALSE.

Let’s say we have “new.html” file with the following content.

<p><strong>Asia</strong> is a <em>continent</em>.</p>

Example

<?php
   $file_pointer= fopen("new.html", "rw");
   echo fgetss($file_pointer);
   fclose($file_pointer);
?>

The following is the output. We haven’t added parameter to avoid stripping of the HTML tags, therefore the output would be the following −

Output

Asia is a continent.

Now, let us see another example wherein we have the same file, but we will add the length and HTML tags parameters to avoid stripping of those tags.

Example

<?php
   $file_pointer = @fopen("new.html", "r");
   if ($file_pointer) {
      while (!feof($handle)) {
         $buffer = fgetss($file_pointer, 1024"<p>,<strong>,<em>");
         echo $buffer;
      }
      fclose($file_pointer);
   }
?>

Output

Asia is a continent.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 24-Jun-2020

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements