Reading a file can not only read the entire file, but also read only one row of data. It can also read one character and a string of the specified length!
Php file processing-read a file (one character, string)
Reading a file can not only read the entire file, but also read only one row of data. It can also read one character and a string of the specified length!
In the previous article "php file processing-how to read a file (one row, the entire file)", we introduced how to read a single row of files and how to explain the entire file, in this article, we will continue to introduce two fgetc () functions and fread () functions!
1. read a character: fgetc () function
When searching and replacing a character in a file, you need to read a specific character. in PHP, you can use the fgetc () function to implement this function. The syntax format of the function is as follows;
string fgetc ( resource $handle )
This function returns a character from the file pointed to by handle. In case of EOF, false is returned.
In the following example, the fgetc () function is used to read the content in the file one by one and output the content. the sample code is as follows:
Output result:
2. read the string of the specified length: fread () function
Fread () can read data of the specified length from a file. the Function Syntax format is as follows:
string fread ( resource $handle , int $length )
The handle parameter is the file to be read, and the length parameter is the number of bytes to be read. The function stops execution when it reads length bytes or reaches EOF.
The following code uses the fread () function to read the content in the file. the sample code is as follows:
"; Echo fread ($ fp, filesize ($ filename); // output other file content?>
The output result is as follows:
This chapter ends. The next article describes how to write data to files and operate files in PHP. For more information, see PHP file processing-writing files and operating files.
The above is the PHP file processing-read the detailed content of the file (a character, string), please pay attention to other related articles in the first PHP community!