Reading Characters: fgetc


Reading Characters: fgetc

Here's another reading functionthe fgetc function, which lets you read a single character from an open file. You can see how this works in Example 7-7, phpfgetc.php, where we're reading the contents of file.txt character by character and echoing it in a browser window.

Example 7-7. Accessing base class methods, phpfgetc.php
 <HTML>     <HEAD>         <TITLE>Reading a file with fgetc</TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>Reading a file with fgetc</H1>             <?php                 $handle = fopen("file.txt", "r");                 while ($char = fgetc($handle)) {                     if($char == "\n"){                         $char = "<BR>";                     }                     echo "$char";                 }                 fclose($handle);             ?>         </CENTER>     </BODY> </HTML> 

The results appear in Figure 7-7 (note that the code changes line endings to <BR>).

Figure 7-7. Reading from a file using fgetc.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net