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

Member Avatar for iamthwee

So it is removing the spaces or tabs?

Member Avatar for iamthwee

I don't think its the php but the html rendering it.

Try adding <pre></pre> tags in your echo.

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.

Member Avatar for iamthwee

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.

you are brilliant - Genius! realy!

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.