473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Best way to parse CSV data?

Hello

I googled for samples, but I'd like to make sure there's no simpler
way to parse tab-separated data read from a web page, before I can
save them into a database:

<?php
$input = file_get_contents ("http://server/data.php");

//item1<TAB>item2<CRLF>
$contents = explode("\r\n", $input);

foreach ($contents as $row) {
$line = explode("\t",$row);
foreach($line as $col) {
//Here, will save each col into database
print $col;
}
}
?>

Thanks for any tip.
Jun 2 '08 #1
9 3080
Gilles Ganault wrote:
Hello

I googled for samples, but I'd like to make sure there's no simpler
way to parse tab-separated data read from a web page, before I can
save them into a database:

<?php
$input = file_get_contents ("http://server/data.php");

//item1<TAB>item2<CRLF>
$contents = explode("\r\n", $input);
You can use
$contents = file("http://server/data.php");

to get the data into an array
foreach ($contents as $row) {
$line = explode("\t",$row);
foreach($line as $col) {
//Here, will save each col into database
print $col;
}
}
?>

Thanks for any tip.

Works as well as anything else.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #2
On Wed, 14 May 2008 17:29:17 -0400, Jerry Stuckle
<js*******@attglobal.netwrote:
> foreach ($contents as $row) {
$line = explode("\t",$row);
foreach($line as $col) {
//Here, will save each col into database
print $col;
}
}
?>

Works as well as anything else.
I ended up simplifying it a bit:

$input = file("http://server/data.php");
foreach ($input as $row) {
//Final CRLF is INDISPENSABLE!
preg_match("|(.+?)\t(.+?)\r\n|",$row,$matches);

//Any way to display the output of prepare() just to check?
$sql = "INSERT INTO mytable (id, name) VALUES (?,?)";
$insert = $dbh->prepare($sql);
$insert->execute(array($matches[1],$matches[2]));
}

I did have a problem with regexing data from file(): Without "\r\n",
the regex doesn't work as planned.

Also, I went through the PDO documentation, but didn't find a method
to display the output of prepare(), which would be convenient for
debugging purposes.

Thanks.
Jun 2 '08 #3
Gilles Ganault wrote:
On Wed, 14 May 2008 17:29:17 -0400, Jerry Stuckle
<js*******@attglobal.netwrote:
>> foreach ($contents as $row) {
$line = explode("\t",$row);
foreach($line as $col) {
//Here, will save each col into database
print $col;
}
}
?>
Works as well as anything else.

I ended up simplifying it a bit:

$input = file("http://server/data.php");
foreach ($input as $row) {
//Final CRLF is INDISPENSABLE!
preg_match("|(.+?)\t(.+?)\r\n|",$row,$matches);

//Any way to display the output of prepare() just to check?
$sql = "INSERT INTO mytable (id, name) VALUES (?,?)";
$insert = $dbh->prepare($sql);
$insert->execute(array($matches[1],$matches[2]));
}

I did have a problem with regexing data from file(): Without "\r\n",
the regex doesn't work as planned.

Also, I went through the PDO documentation, but didn't find a method
to display the output of prepare(), which would be convenient for
debugging purposes.

Thanks.
Yep, if you're going to use a regex, you need the \r\n. But personally
I prefer explode().

And sorry, there's no way to display the output of prepare(). It's out
in the database.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #4
Gilles Ganault wrote:
Hello

I googled for samples, but I'd like to make sure there's no simpler
way to parse tab-separated data read from a web page, before I can
save them into a database:

<?php
$input = file_get_contents ("http://server/data.php");

//item1<TAB>item2<CRLF>
$contents = explode("\r\n", $input);

foreach ($contents as $row) {
$line = explode("\t",$row);
foreach($line as $col) {
//Here, will save each col into database
print $col;
}
}
?>

Thanks for any tip.

Since your goal is to save it to a db (mysql? oracle?) why not use the
tools provided with the db engine and save yourself a ton of headaches
trying to reinvent the wheel...

mysql - search for mysql load file
oracle - search for sql*loader
Access - add it as an external table.
Jun 2 '08 #5
On Wed, 14 May 2008 22:46:58 +0200, Gilles Ganault <no****@nospam.com
wrote:
Hello

I googled for samples, but I'd like to make sure there's no simpler
way to parse tab-separated data read from a web page, before I can
save them into a database:

<?php
$input = file_get_contents ("http://server/data.php");

//item1<TAB>item2<CRLF>
$contents = explode("\r\n", $input);

foreach ($contents as $row) {
$line = explode("\t",$row);
foreach($line as $col) {
//Here, will save each col into database
print $col;
}
}
?>

Thanks for any tip.
<?php
$input = fopen("http://server/data.php",'r');
$result = array();
while($roo = fgetcsv($result,0,"\t")) $result[] = $row;
var_dump($result)
?>

Or, for a database like MySQL, look into LOAD DATA INFILE syntax.
--
Rik Wasmus
[SPAM] Now temporarily looking for some smaller PHP/MySQL projects/work to
fund a self developed bigger project, mail me at rik at rwasmus.nl. [/SPAM]
Jun 2 '08 #6
On Wed, 14 May 2008 20:14:09 -0400, Jerry Stuckle
<js*******@attglobal.netwrote:
>And sorry, there's no way to display the output of prepare(). It's out
in the database.
Too bad. Thanks.
Jun 2 '08 #7
On Wed, 14 May 2008 22:35:11 -0500, Michael Austin
<ma*****@firstdbasource.comwrote:
>Since your goal is to save it to a db (mysql? oracle?) why not use the
tools provided with the db engine and save yourself a ton of headaches
trying to reinvent the wheel...
Forgot to mention the source data is off on a remote server to which I
have no access, hence my need to read data from its web page output,
parse it, before putting data into a second database. Thanks.
Jun 2 '08 #8
On Thu, 15 May 2008 07:46:10 +0200, "Rik Wasmus"
<lu************@hotmail.comwrote:
(snip)

Thanks!
Jun 2 '08 #9
On May 15, 1:21 pm, Gilles Ganault <nos...@nospam.comwrote:
On Wed, 14 May 2008 22:35:11 -0500, Michael Austin

<maus...@firstdbasource.comwrote:
Since your goal is to save it to a db (mysql? oracle?) why not use the
tools provided with the db engine and save yourself a ton of headaches
trying to reinvent the wheel...

Forgot to mention the source data is off on a remote server to which I
have no access, hence my need to read data from its web page output,
parse it, before putting data into a second database. Thanks.
There is no documented standard for 'CSV' (even though it has a
registered mime type) - so each solution needs to be tailored to the
source.

I'm no regex guru but your approach does not seem to allow for escaped
or embedded newlines within field data.

C.
Jun 2 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Peter A. Schott | last post by:
I've got a file that seems to come across more like a dictionary from what I can tell. Something like the following format: ###,1,val_1,2,val_2,3,val_3,5,val_5,10,val_10...
12
by: jacob nikom | last post by:
Hi, I would like to store XML files in MySQL. What is the best solution: 1. Convert it to string and store it as CLOB/text 2. Serialize it and store as byte array 3. Flatten it out and create...
2
by: Jim Williams | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** I'm working on a 3d engine system. I need to save/load data in the systems objects to/from XML files. When I say objects, I mean...
3
by: Andy Fish | last post by:
Hi, I have a webpage with some javascript that constructs some data structures, some of which contain nested structures or arrays of other structures. It's the same order of complexity as a...
19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
1
by: phl | last post by:
Hi, I have some data in an xml file which I need to use in my web form. The data in the xml file needs to be shown in maybe a repeater, grid or data control. I will need to parse the data make...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
19
by: Steve | last post by:
I have to create 2 strings and then parse one string out to save the data into the database. My first string looks like this: ...
6
dennison
by: dennison | last post by:
I am trying to write code for a class that has a couple of attributes that will can only be known upon creation. Because I think accessor functions (e.g. getSomething()) are outdate, I declared my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.