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().
⇒ Issue with "while ($c=fgetc($f))" Loop in PHP
⇐ Reading One Line of Text from a File in PHP
2016-12-02, 1866🔥, 0💬
Popular Posts:
How To Convert a Character to an ASCII Value? If you want to convert characters to ASCII values, you...
How To Read Data from Keyboard (Standard Input) in PHP? If you want to read data from the standard i...
What validation keywords I can use in JSON Schema to specifically validate JSON Array values? The cu...
How to attach console to a Running Container using the "docker container exec" command? I want to ge...
How to use the JSON to XML Conversion Tool at utilities-online.info? If you want to try the JSON to ...