?
Solved

PHP split text file into array

Posted on 2008-10-20
3
Medium Priority
?
5,200 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 1400 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 600 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

765 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