In PHP, the fscanf() function is used to parse input from an open file according to a specified format. It is commonly used to read formatted data such as strings, integers, or floating-point numbers from files.
fscanf(file_pointer, format, mixed)Here, file_pointer is a file resource opened by fopen(), format specifies the reading pattern, and mixed represents optional variables to store the parsed values.
If only two parameters are provided, fscanf() returns the parsed values as an array. If additional parameters are provided, the results are assigned directly to the specified variables.
<?php
$file_pointer = fopen("new.txt", "r");
while ($playerrank = fscanf($file_pointer, "%s\t%d\n")) {
list($name, $rank) = $playerrank;
echo "$name got rank $rank.";
}
fclose($file_pointer);
?>Amit got rank 2The fscanf() function is a powerful tool for reading structured data from files. By using the right formatting parameters, developers can easily extract and process specific types of information from text files, improving both readability and efficiency of PHP applications.