Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #16947

Re: Issue with extracting values from the string (initially from a file)to insert in sql table

Newsgroups comp.lang.php
Date 2016-08-20 08:13 -0700
References <941efc61-cbb4-4c80-bf33-6455bf9828a1@googlegroups.com> <57b850f2$0$911$e4fe514c@news.xs4all.nl>
Message-ID <27fc5bf8-4504-49cf-9e2c-e1f8980f5cc0@googlegroups.com> (permalink)
Subject Re: Issue with extracting values from the string (initially from a file)to insert in sql table
From Alla <modelling.data@gmail.com>

Show all headers | View raw


On Saturday, August 20, 2016 at 3:46:19 PM UTC+3, R.Wieser wrote:
> Alla,
> 
> > I don't understand how to get each value separately.
> 
> Its a basic mixup.  Your code seems to be correct, but for using SINGLE
> quotes around the tab character ( '\t' ).  Single quotes cause that \t to be
> non-interpreted.
> 
> What thus happens is that fgetcsv is looking for a delimiter looking like a
> backslash, followed by the character 't' (and not for the tab character).
> 
> So, change '\t' into "\t" (double-quotes instead of single ones) and all
> will most likely work again.
> 
> Regards,
> Rudy Wieser
> 
> P.s.
> If you wonder how I know this its because I, like most PHP novices, have
> also bumped into it at one time or another ;-)
> 
> 
> -- Origional message:
> Alla <modelling.data@gmail.com> schreef in berichtnieuws
> 941efc61-cbb4-4c80-bf33-6455bf9828a1@googlegroups.com...
> > Hello!
> >
> > I will be grateful for your help on the issue of extracting values from a
> string, which I have obtained
> > from a file.
> >
> > I have stored a file US.txt in my home directory from
> http://download.geonames.org/export/zip/readme.txt
> > Here is the quote from the exercise I am doing: "US.txt is quite like a
> CSV file except that fields
> > are delimited with \t (a tab character) instead of a comma. " Hence, I am
> using fgetcsv() function.
> >
> > Then i have tried to write a code to extract values from this file to be
> able to store them in a sql table.
> > For now I am just testing the code by trying to extract only the first row
> from US.txt and get each
> > variable, but I fail. var_dump() shows me that the whole string is stored
> at the key 0. I don't
> > understand how to get each value separately.
> >
> > Please, take a look at this test code:
> >
> > <?php
> >
> >     $file = "/home/ubuntu/workspace/US.txt";
> >
> > // perform tests on the file
> >     if(!file_exists($file))
> >         echo "Sorry, there is no such file $file\n";
> >     else if(!($open_file = fopen($file, "r")))
> >         echo "Sorry, couldn't open $file\n";
> >     else if(!is_readable($file))
> >         echo "$file is not readable\n";
> >     else if(filesize($file)==0)
> >         echo "Seems there is not content in the file $file\n";
> >     else
> >     {
> >         // store the first row of US.txt in a new variable
> >         $new_entry = fgetcsv($open_file, '\t');
> >         var_dump($new_entry);
> >
> >         /******** var_dump result reveals all values of the first row
> stored as an array at key 0:
> > array(1) {
> >   [0] =>
> >   string(48) "US        34034   APO             AA      Dillon  033
> 33.0364 -82.2493        1"
> > }
> >        *********/
> >
> >         file://store each value in the string in a separate variable
> >          extract($new_entry);
> >
> >         $entry1 = $new_entry[1]; // supposed to equal 'US'; it doesn't
> >         $entry2 = $new_entry[2]; // supposed to equal '34034'; it doesn't
> >         $entry3 = $new_entry[3]; // supposed to equal 'APO'; it doesn't
> >         $entry4 = $new_entry[4]; // supposed to equal 'AA'; it doesn't
> >     }
> > ?>
> >
> > Thank you very much!

Ruby and Christoph, 

Thank you very much! I have corrected my mistakes according to your comments,
and, surely, it worked )

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Issue with extracting values from the string (initially from a file) to insert in sql table Alla <modelling.data@gmail.com> - 2016-08-20 05:06 -0700
  Re: Issue with extracting values from the string (initially from a file)to insert in sql table "R.Wieser" <address@not.available> - 2016-08-20 14:48 +0200
    Re: Issue with extracting values from the string (initially from a file)to insert in sql table Alla <modelling.data@gmail.com> - 2016-08-20 08:13 -0700
      Re: Issue with extracting values from the string (initially from afile)to insert in sql table "R.Wieser" <address@not.available> - 2016-08-20 18:06 +0200
    Re: Issue with extracting values from the string (initially from a file)to insert in sql table Alla <modelling.data@gmail.com> - 2016-08-20 08:30 -0700
  Re: Issue with extracting values from the string (initially from a file) to insert in sql table "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-08-20 14:55 +0200
  Re: Issue with extracting values from the string (initially from a file) to insert in sql table He <y12983@mail.ee> - 2022-02-02 02:23 -0800

csiph-web