Jump to content

[SOLVED] Reading data from a text file into a database


VTS

Recommended Posts

The problem that I am having is finding a way to read data into a database from a textfile that is set up like this:

 

"1 23 23.90 10"

 

Each line begins and ends with the " mark.  I have read data from text files before, but they just had the "\n" at the end of the line.  What do I do when it has a " mark at the beginning and ending of each line?

 

Thanks in advance,

VTS

Link to comment
Share on other sites

Do you, or do you not want the " in the data you enter?

 

If you do, you would probably want to do addslashes() on the line before you input, then stripslashes() when you pull it out.

 

If you don't want them in there I'd suggest using some substringing, a regex, or explode(). I myself would use explode('"',$line), but I'm not sure how well it handles the explode token if it's at the beginning and end of the line.

 

In case you can't tell, the first parameter I passed to explode there was single quote, double quote, single quote.

Link to comment
Share on other sites

Well I do not need the " marks entered in my database.  Those marks are generated by a program that I do not have access to.  I guess I will try using the explode function.

 

thanks for the help,

VTS

Link to comment
Share on other sites

If you're going to use explode I would suggest this (assuming $line is the line of the file read in):

 

$line ="abcd"; //gibberish!

$line .= //read in file (I don't use files often enough to know function call)

$line .="dcba";

$linearr = explode('"',$line);

$line = $linearr[1];

 

That should give you the data inside the quotes.

Link to comment
Share on other sites

Let me add one more thing.  Each line has 4 values in it.  In this line: "1 23 23.90 10"

1, 23, 23.90, and 10 are all seperate fields so I need to read them in seperately.  I tried this:

  $file = "c:\\data.txt";
  if(!file_exists($file)){
   echo "File does not exist";
  }
  $handle = fopen($file, "r");
  $contents = fread($handle, filesize($file));
  fclose($handle);
  $row = explode("\n", $contents);
  $rows = count($row);

  $i = 0;
  while($i < $rows-1){
   $parts = explode('"', $row[$i]);
   unset($numbers);
   $numbers = array();
   foreach($parts as $part) {
    $part = trim($part);
    if(is_numeric($part)) {
 array_push($numbers, $part);
}
   }
   $store = array_pop($numbers);
   $a = array_pop($numbers);
   $b = array_pop($numbers);
   $c = array_pop($numbers);
   echo "</br> $store  $a  $b  $c </br>";
  $i++;
  }

 

but it doesnt seem to work.  I am just using an echo statement so I can see that it is working properly before I try inserting any data into the database.  I probably just overlooked something stupid like I usually do.  Any ideas on why its not working?

Link to comment
Share on other sites

Replace your similar lines with this (make sure to backup your stuff first):

 

$i = 0;

while($i < $rows-1){

$line = "abcd".$row[$i]."dcba";

$parts = explode('"', $line);

unset($numbers);

$numbers = array();

$part = trim($parts[1]);

if(is_numeric($part)) {

array_push($numbers, $part);

}

 

 

See if that works.

Link to comment
Share on other sites

I just found out what it is doing....instead of having one field in $part, it has the whole line.  I guess I need to go a little further with my exploding now and use it to seperate the fields. When I used echo on $part right after I used trim() on it, I get a whole line in it.  I am about to go and try this now and see how it works.

Link to comment
Share on other sites

Ok, I figured it out  :D.  Here is what I used:

  set_time_limit(999);
  $file = "c:\\data.txt";
  if(!file_exists($file)){
   echo "File does not exist";
  }
  $handle = fopen($file, "r");
  $contents = fread($handle, filesize($file));
  fclose($handle);
  $row = explode("\r", $contents);
  $rows = count($row);
  $i = 0;
  while($i < $rows-1){
   $parts = explode('"', $row[$i]);
   unset($numbers);
   $numbers = array();
   foreach($parts as $part) {
    $part = trim($part);
echo "</br>trimed part = $part";
$x = 0;
$partA = explode(" ", $part);  //new lines added here to store each field seperately
foreach($partA as $var){
 if(is_numeric($var)) {
  echo "</br> *var = $var";
  array_push($numbers, $var);
  }
 $x++;
    }
   }
   $store = array_pop($numbers);
   $a = array_pop($numbers);
   $b = array_pop($numbers);
   $c = array_pop($numbers);
   echo "</br> store = $store  || a= $a  || b= $b  || c= $c </br>";
   $i++;
  }

 

Thanks for the help Balmung!

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.