PHP 檔案系統 fgetc() 函式



PHP 檔案系統fgetc()函式用於從開啟的檔案中讀取一個字元。它接受檔案指標作為引數。該函式可以從開啟的檔案中返回單個字元,並從給定的檔案指標獲取一個字元。

此函式可能返回布林值 false,也可能返回計算結果為 false 的非布林值,例如 0 或 " "。

此函式處理非常大的檔案速度非常慢,因此不能用於處理大型檔案。如果我們需要按順序從大型檔案中讀取一個字元,可以使用 fgets() 函式按順序讀取一行資料,然後使用fgetc()函式按順序處理該行資料。

語法

以下是 PHP 檔案系統fgetc()函式的語法:

string fgetc ( resource $handle )

引數

使用fgetc()函式所需的引數如下:

序號 引數及說明
1

handle(必需)

這是指向 FILE 物件的指標,指示從中讀取字元的控制代碼。

返回值

成功時返回字串值,失敗時返回 FALSE。

PHP 版本

fgetc()函式最初作為核心 PHP 4 的一部分引入,並且與 PHP 5、PHP 7、PHP 8 相容。

示例

此 PHP 程式碼向我們展示瞭如何使用 PHP 檔案系統fgetc()函式開啟檔案並從中讀取單個字元。之後關閉檔案。

<?php
   $file = fopen("/PhpProject/sample.txt", "r");
   echo fgetc($file);
   fclose($file);
?>

輸出

這將產生以下結果:

t

示例

此 PHP 程式碼向我們展示瞭如何使用 while 迴圈開啟檔案並讀取每個字元直到檔案結尾,之後關閉檔案。

<?php
   $file = fopen("/PhpProject/sample.txt", "r");
   while(! feof($file)) {
      echo fgetc($file);
   }
   fclose($file);
?>

輸出

這將產生以下結果:

tutorialspoint

示例

由於fgetc()函式返回字串值或失敗時返回 FALSE,因此您可以使用條件表示式來驗證這一點。請檢視下面的程式碼示例:

<?php
   // Open the file in read mode
   $file = fopen("example.txt", "r"); 

   // Check if the file was opened successfully
   if ($file) { 
      // Read characters until the end of the file
      while (($char = fgetc($file)) !== false) { 
         echo $char; // Print each character
      }
      fclose($file); // Close the file
   } else {
      // Error message if file could not be opened
      echo "Could not open file."; 
   }
?>

輸出

這將生成以下結果:

Hello World

Tutorialspoint

Information Security

Web Browser

示例

此 PHP 程式碼計算給定檔案中特定字元或字母出現的次數。因此,我們使用了兩個變數 $charToCount 和 $count,分別初始化為 a 和 0。

<?php
   // Open a file 
   $file = fopen("/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt", "r"); // Open in read mode
   $charToCount = 'a';
   $count = 0;

   if ($file) {
      while (!feof($file)) {
         if (fgetc($file) == $charToCount) {
               $count++;
         }
      }
      // Close the file
      fclose($file); 
      echo "The character '$charToCount' appears $count times in the file.";
   } else {
      // Error message if file could not be opened
      echo "Could not open file."; 
   }
?> 

輸出

這將生成以下結果:

The character 'a' appears 22 times in the file.

示例

在此 PHP 示例程式碼中,我們將嘗試從檔案中讀取字元,並在找到特定字元時停止。我們使用了 $stopChar 變數並將其初始化為點 (.),我們必須在此停止。

<?php
   // Open text file in read mode
   $file = fopen("/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt", "r"); 

   // Particular Character for which we have to stop at
   $stopChar = '.';

   if ($file) {
      while (!feof($file)) {
         $char = fgetc($file);
         if ($char === $stopChar) {
               // Stop reading when the specific character is found
               break; 
         }
         
         // Print each character
         echo $char; 
      }

      // Close the file
      fclose($file); 
   } else {

      // Error message if file could not be opened
      echo "Could not open file."; 
   }
?> 

輸出

以下是上述程式碼的輸出:

Hello World!!!!!

Today is 13 June 2024 and I am here to help you to learn PHP functions

註釋

  • fgetc()方法到達檔案結尾時,它將返回 false。
  • 在呼叫fgetc()之前,始終確保已成功開啟檔案。此外,正確使用 false 以避免無限迴圈或問題。

總結

PHP 的fgetc()函式一次從開啟的檔案中讀取一個字元。失敗時,它返回字串或 false。對於大型檔案,它效率低下;為了獲得更好的效能,請使用 fgets() 讀取行並使用fgetc()處理它們。

php_function_reference.htm
廣告