Reading text files in PHP

WBOY
Release: 2024-02-29 22:12:02
forward
950 people have browsed it

php editor Xigua In PHP development, reading text files is a common operation. Through PHP's file reading function, you can easily open, read and process the contents of text files. Whether reading configuration files, log files, or other types of text files, PHP provides a wealth of functions and methods to achieve this operation. This article will introduce how to read text files in PHP, as well as some common tips and precautions.


Use thefgets()function inphpto read the text file line by line

We can use thewhileLoopedfgets()function reads a text file line by line in PHP. The function returns a row if there is one, or false if there are no more rows to read. It requires two parameters. The syntax is as follows.

fgets($file, $length)
Copy after login

Here,$fileis similar to a file pointer pointing to a successfully opened file.$lengthoption, optional, indicating the number of bytes to read.

We can use theopen()function to read the file, and then use thewhileloop to loop through each line through thefgets()function. We have a text fileabc.txtwith the following content.

Hi How are you Have a great day
Copy after login

For example, create a variable$txt_fileand write thefopen()function in it. Open fileabc.txtinrmode. Create a row counter variable$aand assign the value1to it. Then, create awhileloop. Within the loop's parentheses, write thefgets()function that takes$text_fileas an argument. Assign the function to the$linevariable in the loop. Print the$linevariable and connect it to the$avariable inside the loop body. Add the$avariable and use thefclose()function outside the loop to close the file stream.

Sample code:

"; $a++; } fclose($txt_file); ?>
Copy after login

Output:

1 Hi 2 How are you 3 Have a great day
Copy after login

Use thefile()function in PHP to read the text file line by line

file()The function reads the entire file into anarray.file()The syntax of the function is as follows.

file($filename, $flag, $context)
Copy after login

Here,$filenameis the file path to be read. The$flagoption is optional and consists of various constants such asFILE_USE_INCLUDE_PATH,FILE_IGNORE_NEW_LINES, andFILE_SKIP_EMPTY_LINES. The third, also optional, defines a context resource.

file()Function returns the entire array if it exists or returns false. We can use this function to read the file context line by line through the helo of theforeach()function.foreach()The function will loop through the entire content and extract each row individually.

For example, store the file path in the variable$txt_file. Write a variable$linesand store thefile()function on it using$txt_fileas argument. Next, use theforeach()function to loop through the contents of the file. Use$linesas the iterator and$num=>$lineas the value. Within the body of the loop, print the$numand$linevariables.

Sample code:

$txt_file = 'abc.txt'; $lines = file($txt_file); foreach ($lines as $num=>$line) { echo 'Line '.$num.': '.$line.'
'; }
Copy after login

Output:

Line 0: Hi Line 1: How are you Line 2: Have a great day
Copy after login

Usingfile_get_contents()andexplode()## in PHP # Function reads the file line by line

file_get_contents()The function reads the entire file into astring. Returns the entire file as a string if the content exists, false otherwise. We can specify the file path as an argument to the function.explode()The function splits a string into an array using a delimiter.explode()The syntax of the function is as follows. The

explode(separator, $string, $limit)
Copy after login

separatoroption is used to separate the$stringby the$limitnumber of elements when returning the value. We can combine thefile_get_contents(),explode()andforeach()functions to read a text file line by line in PHP.

For example, create a variable

$contentsand write thefile_get_contents()function on it with the file path in the argument. Use theexplode()function with the\ncharacter as the separator and$contentsas the string. Assign the function to variable$lines. Use aforeachloop to loop$linesinto$line. Then, print$lineinside the loop body.

In the example below, the

$contentsvariable returns a string. We split it into an array with\nnewlines and print each line using a loop.

Sample code:

$contents=file_get_contents("abc.txt"); $lines=explode("\n",$contents); foreach($lines as $line){ echo $line.'
'; }
Copy after login

Output:

Hi How are you Have a great day
Copy after login

The above is the detailed content of Reading text files in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!