Php file processing-read a file (one character, string)

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.