473,569 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CSV parsing

Hello all,

Anyone know of a way to call fgetcsv and fputcsv using a string rather than
a resource? (i.e.., parse a comma delimited string without writing it first
to a file.)

Thanks,
Kevin
Jul 17 '05 #1
6 2330

Kevin Lin wrote (in part):
Anyone know of a way to call fgetcsv and fputcsv using a string rather than a resource? (i.e.., parse a comma delimited string without writing it first to a file.)


Use the explode() function:

<?
$str = explode(',','Th is,is,a,test');
?>

Results in the array $str with four elements:
$str[0] = 'This'
$str[1] = 'is'
$str[2] = 'a'
$str[3] = 'test'

Ken

Jul 17 '05 #2
Thanks for the reply, but explode doesn't handle quoted cases, e.g.:

'Item1,"Item A,ItemB","Items ""one,"" ""two,"" and ""three"""'

should get parsed to:

$array[0]='Item1'
$array[1]='Item A,Item B'
$array[2]='Items "one," "two," and "three"'
"Ken Robinson" <ke******@rbnsn .com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .

Kevin Lin wrote (in part):
Anyone know of a way to call fgetcsv and fputcsv using a string

rather than
a resource? (i.e.., parse a comma delimited string without writing

it first
to a file.)


Use the explode() function:

<?
$str = explode(',','Th is,is,a,test');
?>

Results in the array $str with four elements:
$str[0] = 'This'
$str[1] = 'is'
$str[2] = 'a'
$str[3] = 'test'

Ken

Jul 17 '05 #3
Kevin Lin <ke***@wx3remov e4spam.com> wrote:
Thanks for the reply, but explode doesn't handle quoted cases, e.g.:

[snip]

By reading the user comments on the fgetcsv manual page one can find
http://bu.orbitel.bg/fgetcsvfromline.php. Seems the solution that you
are looking for.

Jul 17 '05 #4

"Kevin Lin" <ke***@wx3REMOV E4SPAM.com> wrote in message
news:hdKod.5574 18$mD.522470@at tbi_s02...
Hello all,

Anyone know of a way to call fgetcsv and fputcsv using a string rather than a resource? (i.e.., parse a comma delimited string without writing it first to a file.)

Thanks,
Kevin


Use the example class at
http://us2.php.net/manual/en/functio...r-register.php
Jul 17 '05 #5
"Kevin Lin" <ke***@wx3REMOV E4SPAM.com> wrote in message news:<hdKod.557 418$mD.522470@a ttbi_s02>...
Hello all,

Anyone know of a way to call fgetcsv and fputcsv using a string rather than
a resource? (i.e.., parse a comma delimited string without writing it first
to a file.)


IMHO, the most dependable way is to use existing fgetcsv along with
a temporary file:

<?php
function ParseCSVString( $str, $delim=',')
{
//write the CSV string to a temporary file so that fgetcsv() can be
used to process...
$fp = tmpfile();
fwrite($fp, $str);
rewind($fp); //rewind to process CSV
$csv_length = strlen($str);
$data_arr = fgetcsv($fp, $csv_length, $delim);
fclose($fp); //clean up temp file
return $data_arr; //return the array
}
?>

Or may use csv_explode() found at <http://in.php.net/explode#37004>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com
Jul 17 '05 #6
This looks useful, thanks.

"Chung Leong" <ch***********@ hotmail.com> wrote in message
news:bK******** ************@co mcast.com...

"Kevin Lin" <ke***@wx3REMOV E4SPAM.com> wrote in message
news:hdKod.5574 18$mD.522470@at tbi_s02...
Hello all,

Anyone know of a way to call fgetcsv and fputcsv using a string rather

than
a resource? (i.e.., parse a comma delimited string without writing it

first
to a file.)

Thanks,
Kevin


Use the example class at
http://us2.php.net/manual/en/functio...r-register.php

Jul 17 '05 #7

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

Similar topics

8
9430
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $ Last-Modified: $Date: 2003/10/28 19:48:44 $ Author: A.M. Kuchling <amk@amk.ca> Status: Draft Type: Standards Track
2
3931
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home Canonicalpath-Directory4: \\wkdis3\ROOT\home\bwe\ You selected the file named AAA.XML getXmlAlgorithmDocument(): IOException Not logged in
16
2869
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after...
0
4108
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We have an schema (s1) on an Oracle 9i database with database links pointing to a schema (s2) on another Oracle 9i database.
9
4047
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics" portions of Babe Ruth page (http://www.baseballprospectus.com/dt/ruthba01.shtml) and store that info in a CSV file. Also, I would like to do this for...
5
4289
by: randy | last post by:
Can some point me to a good example of parsing XML using C# 2.0? Thanks
3
4367
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in the file) with the location. And for a particular section I parse only that section. The file is something like, .... DATAS
13
4475
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
7
2395
by: Daniel Fetchinson | last post by:
Many times a more user friendly date format is convenient than the pure date and time. For example for a date that is yesterday I would like to see "yesterday" instead of the date itself. And for a date that was 2 days ago I would like to see "2 days ago" but for something that was 4 days ago I would like to see the actual date. This is often...
1
4360
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or Stack code and parsing code my professor i does not like me using buffer reader on my code and my professor did even give me an example code for parsing...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.