Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

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

已查看 14 次
跳至第一个未读帖子

Alla

未读,
2016年8月20日 08:06:262016/8/20
收件人
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"
}
*********/

//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!

R.Wieser

未读,
2016年8月20日 08:46:192016/8/20
收件人
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 <modelli...@gmail.com> schreef in berichtnieuws
941efc61-cbb4-4c80...@googlegroups.com...
> file://store each value in the string in a separate variable

Christoph M. Becker

未读,
2016年8月20日 08:55:292016/8/20
收件人
On 20.08.2016 at 14:06, Alla wrote:

> 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.
>
> // store the first row of US.txt in a new variable
> $new_entry = fgetcsv($open_file, '\t');

$delimiter is the *third* parameter of fgetcsv, not the _second_. Also
heed Rudy's advice, that the tab character must be double-quoted. So:

$new_entry = fgetcsv($open_file, 0, "\t");

--
Christoph M. Becker

Alla

未读,
2016年8月20日 11:13:472016/8/20
收件人
Ruby and Christoph,

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

Alla

未读,
2016年8月20日 11:32:042016/8/20
收件人
<snip>

I would like to thank you again because I have spent quite a few hours trying to decipher my
mistake and to understand why the code doesn't work; I have even tried other methods,
like file(), which, surely enough, didn't help me, and is not a better way to extract information
for my purpose.

R.Wieser

未读,
2016年8月20日 12:03:472016/8/20
收件人
Alla,

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

Glad I could help, and thank you for telling us it did. :-)

Regards,
Rudy Wieser



He

未读,
2022年2月2日 05:23:392022/2/2
收件人
Hey

Where are you ?

🙂🙂🙂🙂🙂🙂🙂🙂🙂🙂🙂🙂🙂
0 个新帖子