2

Quick update: The reason I need this solution is that this one php file is used to expand the flat file for about hundred users (that all use the same php file, but have their own flat files)

SOLUTION:

I worked with this one more day, rephrased the question and got a really great answer. I add it here for future help for others:

$content = file_get_contents("newstest.db");
$content = preg_replace('/(^ID:.*\S)/im', '$1value4:::', $content);
$content = preg_replace('/(^\d+.*\S)/im', '$1:::', $content);
file_put_contents("newstest.db", $content);

The original content of the flat file used when testing the code was:

ID:::value1:::value2:::value3:::
1:::My:::first:::line:::
2:::My:::second:::line:::
3:::Your:::third:::line:::

ORIGINAL QUESTION:

I have a PHP script I am trying to modify. Being a PHP newbie, and have searched both here and on Google without finding a solution, I ask here.

I need to add more values (columns) in the flat file, automatically if the "column" does not exist from before.

Because this one PHP file is shared with many users (each with their own flat file), I need a way to automatically add new "columns" in their flat files if the column does not exist. Doing it manually is very time consuming, and I bet there is an easy way.


INFO:

The flat file is named "newstest.db"

The flat file has this layout:

id:::title:::short:::long:::author:::email:::value1:::value2:::value3:::

So the divider is :::

I understand the basics, that I need to add for instance "value4:::" after "value3:::" in the first line of the news.db, then add ::: to the other existing lines to update all lines and prepare for the new "value4"

Today the php uses this to connect to the flat file: ($filesource is the path to the flat file including it's name. Unique for each user.)

$connect_to_file = connect_pb ($filesource);

And to write to the file I use:

insert_pb($filesource,"$new_id:::$title:::$short:::$long:::$author:::$email:::$value1:::::::::");

(As you see in this case value 2 and 3 is not used in this case, but are in others.)


QUESTION:

Is there a quick/ existing php code to use to add a new column if it doesn't already exist? Or do I need to make the php code for this specific task?

I understand that the code must do something along:

  1. If "value4" does not exist in line 0 in $filesource
  2. then add "value4:::" at the end of line 0,
  3. and for each of the other lines add ":::" at the end.

I don't know where to start, but I have tried for some hours.

I understand this:

update_pb(pathtofiletosaveto,"id","x == (ID of news)","value in first line","value to add");

But I don't know how to make an if statement as in 1) above, neither how to update the line 0 in the flat file to add "value4:::" at the end etc.


MY CODE (does not work as intended):

OR, may be I need to read only line 1 in the file (newstest.db), and then exchange that with a new line if "value4" is not in line 1?

A suggestion, but I don't know how do all: (It's probably full of errors, as I have tried to read up and find examples and combining code.)

<?php
// specify the file
$file_source="newstest.db";

// get the content of the file
$newscontent = file($file_source, true);
$lines ='';
// handle the content, add "value4:::" and ":::" to the lines
foreach ($newscontent as $line_num => $linehere) {  
    // add "value4:::" at the end of first line only, and put it in memory  
    if($line_num[0]) {$lines .= $linehere.'value4:::';}
        else {
            // then add ":::" to the other lines and add them to memory
            $lines .= $linehere.':::';
             }
    // echo results just to see what is going on
    //echo 'Line nr'.$line_num.':<br />'.$lines.'<br /><br />';
}

// add

// to show the result
echo "Here is the result:<br /><br />".$lines."<br /><br />";

//Write new content to $file_source
$f = fopen($file_source, 'w');
fwrite($f,$lines);
fclose($f);

echo "done updating database flat file";
?>

This ALMOST works... But it does NOT add "value4:::" to the end of the first line, and it does not add ":::" to the end of the next lines, but to the beginning...

So a couple of questions remains: 1) How can I search in line 0 after "value4", and then write "value4:::" at the end of the line? 2) How can I add ":::" at the end of each line, and not in the beginning?

I kindly ask you to either help me with this.

Preben
  • 1,277
  • 2
  • 11
  • 20
  • Convert it to a CSV, and make your life easier ;) You can replace `:::` with some unique special character that is not found in the file and use that as your separator, then just work with the file as a csv using all the nice built in csv stuff that PHP has, then just replace `:::` back. – Dan Apr 27 '16 at 18:57
  • 1
    See [fgetcsv()](http://php.net/manual/en/function.fgetcsv.php). You can pass '...' as the delimiter parameter, manipulate a row's contents as an array, then write it back with fputcsv(). – Alex Howansky Apr 27 '16 at 18:57
  • @AlexHowansky You can only use a single character as the delimiter. – Dan Apr 27 '16 at 18:58
  • @dan08 Ah bugger, yeah I see that now. Ok then, use [explode()](http://php.net/manual/en/function.explode.php). :) – Alex Howansky Apr 27 '16 at 19:00
  • Hi, I can not convert the file as it is used in different ways in an existing php software. It will be less work to find a solution for this one task I guess. The only thing I am missing is to add "value4:::" to the END of the first line, and then ":::" to the next lines. – Preben Apr 27 '16 at 19:27

1 Answers1

0

Do you absolutely have to use PHP for this task? It seems like something you only need to do once, and is much easier to do in a different way.

For example, if you have a *nix shell with sed, sed -i 's/$/:::/' <file> will do that task for you.

zebediah49
  • 7,467
  • 1
  • 33
  • 50
  • Yes, because there is ONE central PHP file that all my users are using. Each user has their own flat file, and it will be a lot of work fixing this manually. Therefor the $file_source will differ from user to user. – Preben Apr 27 '16 at 19:25