Solved

PHP split text file into array

Posted on 2008-10-20
3
5,159 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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