Solved

PHP split text file into array

Posted on 2008-10-20
3
5,163 Views
Last Modified: 2013-12-13
Hi,

I have a text file that has multiple lines.  What I want to be able to do is split each line down into an array of variables and echo out to a page. e.g.

Text file:
120908 file 1
310808 file 2
240708 some other file name


Display on page as:

Day    Month    Year    Description
12      09          08        file 1
31      08          08        file 2
24      07          08        some other file name

I do not know how to do this though.  I can get it to read and display the complete file, but spliting it into groups and echoing it out remains a mystery,  Fields are set lengths so there are spaces for the remaining character, so I guess it can use substr to select a range.

Thanks,

John
0
Comment
Question by:john-formby
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 350 total points
ID: 22762470
try:
<?php
// Get a file into an array.  In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/yourfile.txt'); 
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
	$day = substr($line,0,2);
	$month = substr($line,2,2);
	$year = substr($line,4,2);
	$description = substr($line,7);
	
	echo "$day $month $year $description";
}
?>

Open in new window

0
 
LVL 8

Expert Comment

by:aldanch
ID: 22762474
You can use the explode() function.

e.g.

$string = "120908 file 1";
$ar = array();
$ar = explode(" ", $string);

You would end up with:

$ar[0] = 120908
$ar[1] = file
$ar[2] = 1


http://us3.php.net/manual/en/function.explode.php
0
 
LVL 8

Assisted Solution

by:MatthiasVance
MatthiasVance earned 150 total points
ID: 22762563
I came up with a solution similar to hielo's, but using tables for the layout.

Kind regards,

Matthias Vance
<table>

<tr>

	<td>Day</td>

	<td>Month</td>

	<td>Year</td>

	<td>Description</td>

</tr>

<?php

	$lines = file("temp.txt");

	foreach($lines as $line) {

		echo "<tr>";

		$day = substr($line, 0, 2);

		$month = substr($line, 2, 2);

		$year = substr($line, 4, 2);

		$desc = substr($line, 7);

		echo "<td>$day</td><td>$month</td><td>$year</td><td>$desc</td>";

		echo "</tr>";

	}
 

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now