Hi,

Your help with would be much appreciated i am new to the world of php...

In this code am taking in a variable from a dynamic form($search). The variable is 10 random unique numbers. It was put into the form from a text/csv file.

In this I need to write each line of the text/csv file into an array except the $search number, delete the contents of the file & write the contents of that array into the initial text file.

The $search variable is always going to be in the text file.

The text file is stuuctured as one number per line....

1234567890
2234567890
3334567890
4444567890
...

the code being used is:

<?

$search = $_POST['used'];

$file = "actn.csv";
$f = fopen($file, "r+");//file pointer

$ammount = count(file($file));// ammount lines/numbers in file

$i = 0;

While(!(feof($f)))

{

$line = fgets($f, 512);// get line

If( $line == $search ) // if line is equal to number passed ignore/dont add to array
{
echo "The number $search has been used.<br><br>";
}

else
{
$i++;
$ctns[$i] = $line; // if not equal add to array
}

}

ftruncate($f,0); // delete content of text file

for ($i=1; $i < $ammount; $i++)
{
fwrite($f, $ctns[$i]); //write ctn array data to file
}

fclose($f);

?>

The problem at the moment is that when writing to the first line of the text file or the array(?) it produces alot of null charachters and adds to them each time.

i.e.

                    0123456789

1123456789
2223456789
3333456789
...

ANY help at all would be great.

Thanks

    sorry the output of the text file should have been...

    "&#09;&#09;&#09;0123456789"
    "1123456789"
    "2223456789"
    "3333456789"

    .....

      try replacing this line:

      $ctns[$i] = $line; // if not equal add to array

      with:

      $ctns[$i] = trim($line); // if not equal add to array

        thanks but no joy with that. I tried it in numerous places: before writing to file and before writing to the array and still those null charachters are going into the first line.

        again another plea for help.......

          if it comes in handy.... ftruncate adds null characters to beginning of a file when it is used. read up about it eventually on the online php manual.

            3 months later

            I was just up agaist kinda the same thing just recently and searching the forums for a solution. When i chanced upon your post.

            Well it is kinda hard to tell what you are trying to do from your example. But, if you are trying to :
            1. take a form_id submitted to you via form. - a 10 elemental array
            2. run through a file_array looking for the form_id submitted to you
            3. remove the form_id element from form_id
            4. then write the form_id out to the filename

            sorry to say but i think that your code is more complex than you need.
            1. you run through file looking for an element of $search
            2. if NOT found add it to the bottom of the file
            3. if found do NOT add it to the file

            you can simplify your process by taking $search and either run through the file for for $search[0] -or- run through $search looking for $file[line]

            regardless the first thing you want to do is to use file() instead of 'while(!(feof($f)))'

            try something like this:

            $search = $_POST['used'];
            $filename = "actn.csv";
            $filelines = file($filename);
            $found = 0 /* set found to false */
            foreach ($filelines as $line_num => $line) {
              foreach ($search as $skey => $sval){
                if (rtrim($line) == $sval) {
                  $found = 1; /* i found the same id, so set found to true */
                  array_splice($search, $skey, 1); /* now remove it from the array */
                }
              }
            }
            

            now search is just left with the lines that are NOT found in the file
            and just append them to the end of the file

            $handle = fopen($filename, 'a');
            fwrite($handle, $search);
            fclose($handle);
            

            ps. in the above example $found does nothing, but in other uses $found can test to find out if you need to write anything to the file or not - if found is false write to file

              Here is another way (untested code) to do this without using ftruncate(). It uses array_diff() to find the values that don't match the search term.

              <?php
              $search     = array($_POST['used']);            // Search term in an array (used for array_diff)
              $file_name  = "actn.csv";                       // File Name
              $file_array = file($file_name);                 // Returns an array, one line per entry
              $remaining  = array_diff($file_array, $search); // Return entries that differ
              $file       = fopen($file_name, "w");           // Open the file for writing
              foreach ($remaining as $line) {                 // Loop through each line not in search
                  fwrite($file, trim($line));                 // Write each line to the file
              }
              fclose($file);                                  // Close the file
              ?>
              
                Write a Reply...