AI编程助手
AI免费问答

PHP中的fscanf()函数

王林   2023-09-07 16:29   936浏览 转载

php中的fscanf()函数

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)

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 “$name got rank $rank.”;
   }
   fclose($file_pointer);
?></p>

Output

Amit got rank 2

php免费学习视频:立即学习
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除