1

I have a text file containing data/fields which are separated by exact column no. Each new line represents a new row of data.

Example of file content:

John Chow 26 543 Avenue Street

From the above, first 10 columns are for the name. Next two are for age. And the rest are for the address.

I need to segregate those data from the uploaded file and display it to the user in a formatted table, which will later on be inserted into the database upon confirmation by user.

I am new to PHP. I think substr could work.

Please guide me on how to go about it. I am using codeigniter with PHP, but basic steps in plain PHP will do. Thanks

4
  • if you used a separator eg John Chow| 26| 543| Avenue Street| it would be sooooooooo much easier, take my name for example... Commented Aug 5, 2011 at 8:39
  • @Lawrence Cherone, the above is the format of the contents I receive. I'll be receiving such files every now and then, and the source says it's generated by their system like that. So can't change the format. I'll have to find a way to sort it using string positions. Commented Aug 5, 2011 at 8:42
  • What if the name is longer than 10 columns? Commented Aug 5, 2011 at 9:07
  • @andho, above is just an example because I couldn't type all the whitespaces for 40 characters. The real file will have 40 characters to store names. So it should be enough for a name. Commented Aug 5, 2011 at 9:50

4 Answers 4

2

Read every line of the file, and parse the lines with either substr or regular expression:

$data = array();
$h = fopen($uploadedfile,"r");
while (false !== ($line = fgets($h)))
{
    /* substring way: */
    $data[] = array(
        'name' => trim(substr($line,0,10)),
        'age' => intval(sbstr($line,10,2),10),
        'address' => trim(substr($line,12))
    );

    /* regular expression way: */
    preg_match("'^(.{10})(.{2})(.*)$'",$line,$n);
    $data[] = array(
        'name' => trim($n[1]),
        'age' => intval($n[2],10),
        'address' => trim($n3)
    );
}
fclose($h);

Then iterate the $data array to display it in a table form.

Edit: what you ask in the comments can be done also with regular expressions. If $val is the parsed 10 character string, then:

$val = preg_replace("'^0*'","",$val);

would replace all leading 0s.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you inti. I Think this should work. One field is 10 characters and denotes an amount. If the amount is 100, the field will contain 0000000100. How can I remove the preceding Zeros?
Edited my answer, according to your request.
You can use file("/path/to/file") to return all the lines in an array, so like $lines = file($uploadedfile); foreach($lines as $line) // do kool stuff
@andreas nice move, but do we always have enough memory to read the full file at once? And create a separate parsed array with the same amount of data.
1

Yes, you should be using substr. This extracts part of the string. Something like:

$name = trim(substr($line, 0, 10));
$age = trim(substr($line, 10, 2));
$addr = trim(substr($line, 12));

I've added trim to remove any extra whitespace.

Comments

0

Umm I suggest you don't use substr, because peoples names are not always going to be the same length so this will create some problems. I think the explode and implode functions would do the trick better for you, they split a string into an array and back.

http://php.net/manual/en/function.explode.php

http://www.php.net/manual/en/function.implode.php

<?PHP

$str = "John Chow 26 543 Avenue Street";
$data = explode(" ", $str);

$first_name = $data[0];
$last_name = $data[1];
$age = $data[2];

$address = implode(" ", array_slice($data, 3));

?>

7 Comments

Sometimes the person may not have a second name. If thats the case, #last_name will probably end up with "26", wouldn't it? I think I should use substr, as the columns are fixed,for eg the name will have 10 columns, if the name is short, the rest of the columns will be whitespace and after the 10th column, the second field will start.
That said, we can't also be sure that users only have first and last name right? What if they have middle name?
Ahh I see, I just assumed that there would be people with names longer than just 10 chracters and that everyone would have a last name. But if you're certain the columns are fixed size then ofcourse, substr is the way to go!
@andreas: Each field will have a fixed length of columns. If name is shorter than that, the rest of it will be whitespace. So I feel its safe to grab the data according to string positions.
@Damchey, sounds good. Just be sure to use trim() on the the substrings to get rid of any whitespaces like inti showed in his/her example.
|
0

You can use a regexp:

if (preg_match('/(.{10})(.{2})(.{12})/', $input, $output)) {
  var_dump($output);
}

1 Comment

Ah .. doh. I didn't see that inti already supplied a regexp version in his answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.