Solved

PHP split text file into array

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php function to remove a file 31 39
How can I update information to the database when there information is dynamic not static? 18 43
PHP and google maps 13 42
PHP AJAX JSON 2 57
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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

813 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

13 Experts available now in Live!

Get 1:1 Help Now