Home > Backend Development > PHP Tutorial > fscanf() function in PHP

fscanf() function in PHP

王林
Release: 2023-09-07 16:30:01
forward
943 people have browsed it

fscanf() function in PHP

The fscanf() function parses input from an open file according to a specified format. It returns the values parsed as an array, if only two parameters were passed.

Syntax

fscanf(file_pointer, format, mixed)
Copy after login

Parameters

  • file_pointer − A file system pointer resource created using fopen().

  • format − Specify the format. Here are the values:

    • %% - Returns a percent
    • %b - Binary number
    • %c - The character according to the ASCII value
    • %f - Floating-point number
    • %F - Floating-point number
    • %o - Octal number
    • %s - String
    • %d - Signed decimal number
    • %e - Scientific notation
    • %u - Unsigned decimal number
    • %x - Hexadecimal number for lowercase letters
    • %X - Hexadecimal number for uppercase letters
  • mixed − Specify the assigned values. Optional.

Return

The fscanf() function returns the values parsed as an array, if only two parameters were passed.

Example

<?php
   $file_pointer = fopen("new.txt", "r");
   while ($playerrank = fscanf($handle, "%s\t%d</p><p>")) {
      list ($name, $rank) = $playerrank;
      echo &ldquo;$name got rank $rank.&rdquo;;
   }
   fclose($file_pointer);
?>
Copy after login

Output

Amit got rank 2
Copy after login

The above is the detailed content of fscanf() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template