Php reading file summary_php tutorial

Source: Internet
Author: User
Php file reading summary. One of the ways to deal with modern programming languages such as PHP is that there are plenty of options available. PHP can easily win the Perl motto "There lets us calculate how many methods There are

One of the fun of dealing with modern programming languages such as PHP is that there are plenty of options available. PHP can easily win Perl's motto "Theres more than one way to do it" (not just one way to do it), especially in file processing. But among the many available options, which is the best tool for job completion? Of course, the actual answer depends on the object to be parsed, so it is worth the time to explore all options.


--------------------------------------------------------------------------------


Traditional fopen method

The fopen method may be the most familiar to C and C ++ programmers in the past, because if you have used these languages, they are more or less tools you have mastered for many years. For any of these methods, open the file by using the standard fopen (function for reading data) method, and then close the file using fclose, as shown in listing 1.


Listing 1. open and read files with fgets

$ File_handle = fopen ("myfile", "r ");
While (! Feof ($ file_handle )){
$ Line = fgets ($ file_handle );
Echo $ line;
}
Fclose ($ file_handle );

Although most programmers with years of programming experience are familiar with these functions, let me break them down. Perform the following steps effectively:

Open the file. $ File_handle stores a reference to the file itself.
Check whether you have reached the end of the file.
Continue to read the file until it reaches the end of the file, while reading and printing each line.
Close the file.
Remember these steps, and I will review every file function used here.

Fopen

The fopen function creates a connection to the file. The reason why I say "create a connection" is that in addition to opening a file, fopen can also open a URL:

$ Fh = fopen ("http: // 127.0.0.1/", "r ");

This line of code creates a connection to the above page and allows you to start reading it like reading a local file.

Note: The "r" used in fopen indicates that the file is opened in read-only mode. Since writing data to a file is not covered in this article, I will not list all other options. However, for cross-platform compatibility, you should change "r" to "rb ". You will see this example later.

Feof

The feof command checks whether you have read the end of the file and returns True or False. The loop in listing 1 continues until you reach the end of the file "myfile. Note: If the URL is read and the socket times out because no data is available for reading, feof returns False.

Fclose

Jump forward to the end of listing 1, and fclose will implement the opposite function: it will close the connection to the file or URL. After this function is executed, you will no longer be able to read any information from a file or socket.

Fgets

When you skip several lines in listing 1, you get to the core of file processing: actually reading the file. The fgets function is the preferred weapon for processing the first example. It extracts a row of data from the file and returns it as a string. After that, you can print or process data in other ways. The example in listing 1 will print the entire file in detail.

If you decide to limit the size of the data block to be processed, you can add a parameter to fgets to limit the maximum length of the row. For example, use the following code to limit the line length to 80 characters:

$ String = fgets ($ file_handle, 81 );

Recall the Terminator at the end of the "" string in C and set the length to a number greater than the actual value. Therefore, if 80 characters are required, 81 is used in the preceding example. This additional character should be added as long as this function is restricted.

Fread

The fgets function is only available for reading multiple files. It is a more common function, because row-by-row parsing usually makes sense. In fact, several other functions can provide similar functions. However, you do not always need to parse lines by line.

Fread is required. The fread function is slightly different from fgets in terms of processing objectives: it tends to read information from binary files (that is, files that do not primarily contain readable human text. Because the concept of "row" is irrelevant to binary files (logical data structures are generally not terminated by new lines), you must specify the number of bytes to read.

$ Fh = fopen ("myfile", "rb ");
$ Data = fread ($ file_handle, 4096 );

Use binary data
Note: the example of this function uses a parameter slightly different from that of fopen. When processing binary data, always remember to include Option B in fopen. If you skip this step®Windows®The system may not be able to process files correctly because they will process new rows in different ways. If Linux®System (or another UNIX®Variant), it may seem irrelevant. However, even if it is not developed for Windows, it will obtain good cross-platform maintainability and be a good habit to be followed.

The above code reads 4,096 bytes (4 KB) of data. Note: fread does not read more than 8,192 bytes (8 KB) no matter how many bytes are specified ).

Assuming that the file size does not exceed 8 KB, the following code should be able to read the entire file into a string.

$ Fh = fopen ("myfile", "rb ");
$ Data = fread ($ fh, filesize ("myfile "));
Fclose ($ fh );

If the file length is greater than this value, you can only read the remaining content in a loop.

Fscanf

Back to string processing, fscanf also follows the traditional C-file library function. If you are not familiar with it, fscanf will read the field data from the file into the variable.

List ($ field1, $ field2, $ field3) = fscanf ($ fh, "% s ");

The format strings used by this function are described in many places (such as PHP.net), so we will not repeat them here. In this case, string formatting is extremely flexible. It is worth noting that all fields are placed in the return value of the function. (In C, they are all passed as parameters .)

Fgetss

The fgetss function is different from the traditional file function and enables you to better understand the power of PHP. This function is similar to the fgets function, but will remove any HTML or PHP Mark found, leaving only plain text. View the following HTML file.


List 2. example HTML file


My title

If you understand what "Cause there aint no one for give you no pain"
Means then you listen to too much of the band America



Then filter it using the fgetss function.


Listing 3. using fgetss

$ File_handle = fopen ("myfile", "r ");
While (! Feof ($ file_handle )){
Echo = fgetss ($ file_handle );
}
Fclose ($ file_handle );

The following is the output:

My title

If you understand what "Cause there aint no one for give you no pain"
Means then you listen to too much of the band America

Fpassthru function

Regardless of how the file is read, you can use fpassthru to dump the remaining data to the standard output channel.

Fpassthru ($ fh );

In addition, this function prints data, so you do not need to use variables to obtain data.

Non-linear file processing: Skip access

Of course, the above functions only allow sequential reading of files. More complex files may require you to jump back and forth to different parts of the file. In this case, fseek is used.

Fseek ($ fh, 0 );

The above example will jump back to the beginning of the file. If you do not need to return completely -- we can set to return kilobytes -- then we can write like this:

Fseek ($ fh, 1024 );

You have some other options since PHP V4.0. For example, if you want to jump forward 100 bytes from the current position, you can try to use:

Fseek ($ fh, 100, SEEK_CUR );

Similarly, you can use the following code to jump back to 100 bytes:

Fseek ($ fh,-100, SEEK_CUR );

If you need to jump back to the first 100 bytes at the end of the file, use SEEK_END.

Fseek ($ fh,-100, SEEK_END );

After arriving at a new location, you can use fgets, fscanf, or any other method to read data.

Note: fseek cannot be used for processing files that reference URLs.


--------------------------------------------------------------------------------


Extract the entire file

Now, we will have access to some of the more unique PHP file processing functions: Use one or two lines to process large pieces of data. For example, how to extract a file and display all its content on a Web page? Okay. you can see the example of using loop in fgets. But how can this process be made simpler? Using fgetcontents will make the process super simple. this method will put the entire file into a string.

$ My_file = file_get_contents ("myfilename ");
Echo $ my_file;

Although it is not the best practice, you can write this command more concisely:

Echo file_get_contents ("myfilename ");

This article describes how to process local files, but it is worth noting that you can use these functions to extract, Echo, and parse other Web pages.

Echo file_get_contents ("http: // 127.0.0.1 /");

This command is equivalent:

$ Fh = fopen ("

One of the fun of NLP processing modern programming languages such as PHP is that there are plenty of options available. PHP can easily win the Perl motto "There...

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.