Home >Backend Development >PHP Tutorial >How to read files into array in php?
PHPRead filesYou can read files in the current server or remote server. The steps are: open the file, read the file and close the file. This article mainly introduces how to read files into array in php? , involving the functions such as file and rtrim in PHP, the operating skills for files and strings. The specific analysis is as follows:
In php, the file can be read into an array through the file() function. The elements in the array are each line of the file. The file() function presses "\n" The line-split file is saved to an array, so each element of the array ends with "\n", which we can remove through the rtrim() function.
rtrim() function removes whitespace characters or other predefined characters on the right side of a string.
php reads the file into the array example code
<?php $lines = file("/tmp/file.txt"); foreach ($lines as $line) { $line = rtrim($line); print("$line\n"); // more statements... } ?>
The above is the detailed content of How to read files into array in php?. For more information, please follow other related articles on the PHP Chinese website!