0

Hi

I read a text file using PHP, the file gets uplaoded, and what I want to do is read the file take some information out of it and post it to mysql.

Problem is that when reading the file it removes the spaces, I cannot count where the data is as addresses and names in the text file are not the same length.

Code to read the file:

<?php


$data = file_get_contents("tmp/file1.txt"); //read the file
$convert = explode("\n", $data); //create array separate by new line

for ($i=0;$i<count($convert);$i++)  
{
    echo $convert[$i].', '; //write value by index
}

?>

The process is to to read the file, get the Date, there are lines it needs to read, each line has 4 Items I need, Doc Number, Order number, User and Amount.

The document layout looks like the text documant attached

The fields are in the are place every time, so for example the date will alwys start at a certain character count from the left.

What I am getting so far is all the spaces have been remoevd and everything is in one continous line.
This is a proble when the name is Fred or Alexander the great, the date is ok, but the line data moves around

Here is my result:

Date:2012/10/01 , Contact perosn: Mikee , Contact Number: 0123456789 , , Payee Name :Fred Flinstone , Payee Address :Rocville , :Rock Three , :Stone 172 , , Source Doc Order User Amount , Number Number , --------------------------------------------------------------- , 2134255 004352 Lisa Simpson 4,050.00 , 3321762 337924 Mickey Mouse 702.35 , , ------------ , TOTAL 4,752.35 , ------------ , , ,

Is there a way to pull the spaces trough too, so i can just do a count, or can I read it in another way.
We receive the textfiles as is.

Mike

Attachments
===============================================================
                                                Date:2012/10/01
Contact perosn: Mikee
Contact Number: 0123456789

Payee Name    :Fred Flinstone
Payee Address :Rocville
              :Rock Three
              :Stone 172
              
Source Doc               Order        User            Amount  
Number                   Number 
---------------------------------------------------------------
2134255                  004352       Lisa Simpson     4,050.00
3321762                  337924       Mickey Mouse       702.35

                                                   ------------
                                               TOTAL   4,752.35
                                                   ------------
===============================================================
2
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by branding4you
0

I tried this

echo $convert[$i].'<br> '; 

it works, yet the spaces are stil missing.
I am going to try running it like this on the 347 files I have, hopefully it will import the data and post in correct fields.

0

What no? Well adding a <br> would be useful. But this has nothing to do with PHP it's simply to do with the rendering in your web browser.

If you add <pre> tags before you echo it out it will show you what you expect...

<?php
$data = file_get_contents("tmp/file1.txt"); //read the file
$convert = explode("\n", $data); //create array separate by new line
echo("<pre>");
for ($i=0;$i<count($convert);$i++)  
{
    echo $convert[$i];
    echo ("<br>");
}
echo("</pre>");
?>

Please mark as solved if this answers your question.

Edited by iamthwee

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.