Tools, FAQ, Tutorials:
Reading a Text File to an Array in PHP
How To Read a Text File into an Array in PHP?
✍: FYIcenter.com
If you have a text file with multiple lines, and you want to read those lines into an array, you can use the file() function. It opens the specified file, reads all the lines, puts each line as a value in an array, and returns the array to you. Here is a PHP script example on how to use file():
<?php
$lines = file("/windows/system32/drivers/etc/services");
foreach ($lines as $line) {
$line = rtrim($line);
print("$line\n");
# more statements...
}
?>
This script will print:
# This file contains port numbers for well-known services echo 7/tcp ftp 21/tcp telnet 23/tcp smtp 25/tcp ...
Note that file() breaks lines right after the new line character "\n". Each line will contain the "\n" at the end. This is why we suggested to use rtrim() to remove "\n".
Also note that, if you are on Unix system, your Internet service file is located at "/etc/services".
⇒ Reading the Entire File to a String in PHP
⇐ Reading and Writing Files in PHP
2016-12-04, ∼2591🔥, 0💬
Popular Posts:
How To Copy Array Values to a List of Variables in PHP? If you want copy all values of an array to a...
How to create a "Sign-up or Sign-in" user flow policy in my Azure AD B2C directory? If you want to b...
FYIcenter.com Online Tools: FYIcenter JSON Validator and Formatter FYIcenter JSON to XML Converter F...
How to detect errors occurred in the json_decode() call? You can use the following two functions to ...
How to use the "forward-request" Policy Statement to call the backend service for an Azure API servi...