Tools, FAQ, Tutorials:
Reading One Character from a File in PHP
How To Read One Character from a File in PHP?
✍: FYIcenter.com
If you have a text file, and you want to read the file one character at a time, you can use the fgetc() function. It reads the current character, moves the file pointer to the next character, and returns the character as a string. If end of the file is reached, fgetc() returns Boolean false. Here is a PHP script example on how to use fgetc():
<?php $file = fopen("/windows/system32/drivers/etc/services", "r"); $count = 0; while ( ($char=fgetc($file)) !== false ) { if ($char=="/") $count++; } fclose($file); print("Number of /: $count\n"); ?>
This script will print:
Number of /: 113
Note that rtrim() is used to remove "\n" from the returning string of fgets().
2016-12-02, 836👍, 0💬
Popular Posts:
What Is session_register() in PHP? session_register() is old function that registers global variable...
How to write a policy to set and get custom variables? Here is a policy that sets and gets custom va...
How to start Visual Studio Command Prompt? I have Visual Studio 2017 Community version with Visual C...
How to view API details on the Publisher Portal of an Azure API Management Service 2017 version? You...
How To Submit Values without Using a Form in PHP? If you know the values you want to submit, you can...