Jump to content

[SOLVED] Writing Files


RabPHP

Recommended Posts

Greetings all,

 

I have to write a file format that requires some very specific data in specific positions on specific lines.

 

For example...

 

I have a list of data with the following requirements:

 

First Name (Line 1 position 1)

Last Name (Line 1 position 45)

Street Address (Line 2 Position 1)

City (Line 2 position 40)

State (Line 2 position 55)

Zip (Line 2 Position 58)

Phone Number (Line 3 Position 1)

Fax Number (Line 3 Position 11)

 

I have tried a combination of fputs, fseek however fseek keeps going back to the first line.  This will be a new file each time therefore the lines do not exist yet to put into an array.

 

Any advise greatly appreciated.

 

Rab

 

 

 

Link to comment
Share on other sites

Do something like this, then write out line by line...

$1 = $fname;
$1 = str_pad($1, 44);
$1 .= $lname;

$2 = $street;
$2 = str_pad($2, 39);
$2 .= $city;
$2 = str_pad($2, 54);
$2 .= $state;
$2 = str_pad($2, 57);
$2 .= $zip;


$3 = $tel;
$3 = str_pad($3, 10);
$3 .= $fax;

 

However what would happen if I lived here 'Llandrindod Wells'?

Link to comment
Share on other sites

Try

 

<?php
$formated = sprintf("%-44.44s", $firstname)."$lastname\n";
$formated .= sprintf("%-39.39s", $street).sprintf("%-15.15s", $city).sprintf("%-3.3s", $state)."$zip\n";
$formated .= sprintf("%-10.10s", $phone)."$fax\n";
?>

 

This will truncate any variable length longer than allowed.

 

 

Link to comment
Share on other sites

If you have multiple lists then you could use this:

<?php

$format = array( array(45, 20),
                 array(40, 15, 3, 6),
                 array(11, 12) );

function getLineData($linedata, $line)
{
    global $format;

    $i = 0;
    foreach($format[$line] as $k => $offset)
    {
        $tmp[] = trim(substr($linedata, $i, $offset));
        $i += $offset;
    }

    return implode("\n", $tmp);
}

$data = file('details.txt');
$tmp = null;

for($i = 0; $i <= (count($data)-1); $i++)
{
    for($k = 0; $k < 3; $k++)
    {
        $tmp .= "\n" . getLineData($data[$i++], $k);
    }

    $details[] = $tmp; $tmp = null; $i--;
}

foreach($details as $c_details)
{
    list(,$firstnane, $lastname, $addr, $city, $state, $zip, $phone, $fax) = explode("\n", $c_details);

    echo "$firstnane $lastname<br />
$addr<br />
$city<br />
$state $zip<br />
$phone $fax
<hr />\n";
}

?>

 

details.txt format:

First Name1                                  Last Name1
Street Address1                         City           ST Zip
Phone #5   Fax Number1
First Name2                                  Last Name2
Street Address4                         City           ST Zip
Phone #4   Fax Number2
First Name3                                   Last Name3
Street Address3                         City           ST Zip
Phone #3   Fax Number3

Link to comment
Share on other sites

Try

 

<?php
$formated = sprintf("%-44.44s", $firstname)."$lastname\n";
$formated .= sprintf("%-39.39s", $street).sprintf("%-15.15s", $city).sprintf("%-3.3s", $state)."$zip\n";
$formated .= sprintf("%-10.10s", $phone)."$fax\n";
?>

Refactoring

<?php
$formated = sprintf("%-44.44s%s\n", $firstname, $lastname);
$formated .= sprintf("%-39.39s%-15.15s%-3.3s%s\n", $street, $city, $state, $zip);
$formated .= sprintf("%-10.10s%s\n", $phone, $fax);
?>

Link to comment
Share on other sites

Greetings all,

 

I have to write a file format that requires some very specific data in specific positions on specific lines.

 

For example...

 

I have a list of data with the following requirements:

 

First Name (Line 1 position 1)

Last Name (Line 1 position 45)

Street Address (Line 2 Position 1)

City (Line 2 position 40)

State (Line 2 position 55)

Zip (Line 2 Position 58)

Phone Number (Line 3 Position 1)

Fax Number (Line 3 Position 11)

 

I have tried a combination of fputs, fseek however fseek keeps going back to the first line.  This will be a new file each time therefore the lines do not exist yet to put into an array.

 

Any advise greatly appreciated.

 

Rab

 

This works perfectly and allows me to seperate everything out nice and neat, thanks!

 

Rab

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.