Solved

PHP split text file into array

Posted on 2008-10-20
3
5,174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

763 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