Re: [PHP] fgetcsv Help

2002-12-12 Thread @ Edwin
Hello,

Justin French [EMAIL PROTECTED] wrote:

[snip]
 Sample line from your CSV should look like this:
 
 ---
 1,foo,harry said \what is it?\,foo
 1,bah,\don't know\ said sally,something
 ---
 
 When echoing these values to the browser, you would strip the slashes.
[/snip]

The double quotes aren't really necessary unless
1. You have a comma in the field
2. You have double quotes in the field

So,

  1,foo,Harry said what is it,foo

would have no problem with fgetcsv().

And, btw, the  are also escaped with another .

- E

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] fgetcsv Help

2002-12-11 Thread DL Neil
Richard,

 I am parsing a csv file with fgetcsv and the fields are surrounding by
 double quotes, now I am running into a problem periodically that when
there
 are quotes within the value it is treating it like another value instead
of
 the same value.
 Any ideas on how to get around that?

 I am thinking I might have to convert all quotes to quot; and then turn
all
 quot;,quot; back into quotes and then the first quot; in the file and
 then last one since obviously they will not have a comma in between them.
 This seems like a while lot of work..
 Ideas?


The first idea is that the system that outputs a .CSV with quotes within a
quoted field is broken big-time and that's the part that needs fixing!

I'm on the same wavelength as you describe: you can't use any automated csv
function on such a file. Quotes should only appear in pairs. The closing
quote must therefore appear immediately prior to a field separator (eg
comma) - whitespace not withstanding, unless it is the last field on a line,
in which case it will appear immediately prior to a record separator.

One possibility is to write a 'filter' script to do this - and thus leave
your existing code intact. The filter could take in the file, explode on
quotes, and then cycle through the resultant array looking at the first
non-whitespace data in every second element, clean/recode where necessary,
and then implode and output.

Alternatively could join/unset consecutive array elements which have been
split inappropriately (by 'internal' quotes), and then implode using the
other style of quotes/another separator, if this can be made to work later
in the process...

What a palaver!
=dn


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] fgetcsv Help

2002-12-11 Thread Justin French
How is the CSV being generated?  Seems to me like your problem isn't
ggetcsv(), but rather the file itself.

Commonly, a CSV file is a series of values, separated by a comma (duh!!).

The separated values are generally enclosed in double quotes (), as it
would appear yours are.

Any double quotes within each value (eg: she said hello) are escaped with
a slash (eg: she said \hello\).

If you are generating the CSV, then you can addslashes() to each value to
escape the quotes, and then later on, stripslashes() to get rid of them.


Sample line from your CSV should look like this:

---
1,foo,harry said \what is it?\,foo
1,bah,\don't know\ said sally,something
---

When echoing these values to the browser, you would strip the slashes.


Cheers,

Justin


on 11/12/02 7:13 PM, Richard Baskett ([EMAIL PROTECTED]) wrote:

 I am parsing a csv file with fgetcsv and the fields are surrounding by
 double quotes, now I am running into a problem periodically that when there
 are quotes within the value it is treating it like another value instead of
 the same value.
 
 Any ideas on how to get around that?
 
 I am thinking I might have to convert all quotes to quot; and then turn all
 quot;,quot; back into quotes and then the first quot; in the file and
 then last one since obviously they will not have a comma in between them.
 This seems like a while lot of work..
 
 Ideas?
 
 Rick
 
 A wise women once said:  No one can help everybody, but everybody can help
 somebody. - Unknown
 

Justin French

http://Indent.com.au
Web Development  
Graphic Design



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] fgetcsv Help

2002-12-11 Thread Richard Baskett
I did email the company that the csv feed is coming from so we'll see what
comes of that.  I really hope they fix it.

Well here is what I did to solve the problem:

I pulled the csv file in using file(), then found the string length, used
substr() to get rid of the first double quote in the line and the newline
and double quote at the end of the line.  Then I replaced all double quotes
with it's html entity equivalent.  Then replaced quot;,quot; back into ,
since that means those quotes are supposed to be there.. hopefully :)

then I just stuck quotes on the beginning and end of the line.

Created a new file and flushed the data into a new file.

If anybody needs the code, I can send it to them.  Hopefully though the
company will get this fixed!  Cheers!

Rick

 I am parsing a csv file with fgetcsv and the fields are surrounding by
 double quotes, now I am running into a problem periodically that when there
 are quotes within the value it is treating it like another value instead of
 the same value.
 Any ideas on how to get around that?
 
 I am thinking I might have to convert all quotes to quot; and then turn all
 quot;,quot; back into quotes and then the first quot; in the file and
 then last one since obviously they will not have a comma in between them.
 This seems like a while lot of work..
 Ideas?

The happiest people are those who think the most interesting thoughts.
Those who decide to use leisure as a means of mental development, who love
good music, good books, good pictures, good company, good conversation, are
the happiest people in the world. And they are not only happy in themselves,
they are the cause of happiness in others. - Phelps


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php