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, 1265👍, 0💬
Popular Posts:
How to use the "rewrite-uri" Policy Statement for an Azure API service operation? The "rewrite-uri" ...
How to read Atom validation errors at w3.org? If your Atom feed has errors, the Atom validator at w3...
How to include additional claims in Azure AD v2.0 id_tokens? If you want to include additional claim...
How To Read a File in Binary Mode in PHP? If you have a file that stores binary data, like an execut...
Can Two Forms Be Nested? Can two forms be nested? The answer is no and yes: No. You can not nest two...