2023-08-29

PHP中的fgetss()函数

PHP中的fgetss()函数

fgestss() 函数从文件指针获取一行并去除 HTML 和 PHP 标签。 fgetss() 函数返回从句柄指向的文件中读取的最大长度为 1 个字节的字符串,其中所有 HTML 和 PHP 代码都被条带化。如果发生错误,则返回 FALSE。

语法

fgetss(file_path,length,tags)
登录后复制

参数

  • file_pointer – 文件指针必须有效,并且必须指向由 fopen() 成功打开的文件或 fsockopen()(尚未由 fclose() 关闭)。

  • length – 数据长度

  • 标签 – 您不想删除的标签。

返回

fgetss() 函数返回从句柄指向的文件中读取的最大长度为 1 个字节的字符串,其中所有 HTML 和 PHP 代码都被条带化。如果发生错误,则返回 FALSE。

假设我们有包含以下内容的“new.html”文件。

<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);
?>
登录后复制

以下是输出结果。我们没有添加参数以避免剥离HTML标签,因此输出结果如下:

输出

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);
   }
?>
登录后复制

输出

Asia is a continent.
登录后复制
登录后复制

以上就是PHP中的fgetss()函数的详细内容,更多请关注php中文网其它相关文章!

https://www.php.cn/faq/599475.html

发表回复

Your email address will not be published. Required fields are marked *