Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How To Read a Text File into an Array
How To Read a Text File into an Array? - PHP Script Tips - Reading and Writing Files
✍: 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 rtrime() to remove "\n".
Also note that, if you are on Unix system, your Internet service file is located at "/etc/services".
2007-04-22, 5166👍, 0💬
Popular Posts:
What is NullPointerException and how to handle it? When an object is not initialized, the default va...
I have 5 questions please give me the answer ,explanation,suggestions if any?? what is PMP(project m...
.NET INTERVIEW QUESTIONS - What are types of compatibility in VB6? There are three possible project ...
How do we create DCOM object in VB6? Using the CreateObject method you can create a DCOM object. You...
How To Call a Sub Procedure? - Oracle DBA FAQ - Creating Your Own PL/SQL Procedures and Functions To...