Link to home
Start Free TrialLog in
Avatar of dplinnane
dplinnaneFlag for United States of America

asked on

fgetcsv and handling values with a dollar sign ie $2.81

JDS UNIPHASE CORP                    1/29/2003  SALE      \2,800.00\ $2.81     \"7,857.77\"  CUSIP: 46612J101
NORTEL NETWORKS CORP NEW    3/13/2003  SALE       840         $2.11      \"1,762.35\"  CUSIP: 656568102

above is a fixed width file.
My code does the following parses flle based on widths user entered, trims white space and concatenates back together so row 1 and 2  looks like this
IMPORT and fixed to csv
JDS UNIPHASE CORP`~`1/29/2003SALE`~` \2,800.00`~`\ $2.81`~`\"7,857.77`~`\"CUSIP: 46612J101                  //ROW 1
NORTEL NETWORKS CORP NEW`~`3/13/2003`~`SALE`~`840`~`$2.11\`~`"1,762.35\`~`"CUSIP: 656568102      // ROW 2

using fgetcsv delim = ~ and txt qualifier = `
JDS UNIPHASE CORP 1/29/2003 SALE \2,800.00\`~ \"7,857.77\"                                   //ROW 1
NORTEL NETWORKS CORP NEW 3/13/2003 SALE 840 $2.11 \"1,762.35\"                        //ROW 2

It seems like the $2.81 variable ROW 1 is being treated like a variable because of the backslash and it seems to disappear. ROW 2 $2.11 appears as normal.  Does anyone have any ideas what is going on? The application I am using (datareformatics.com) uses a feature where I convert a fixed width file to csv, I have it temporarily disabled until I resolve this problem.     Thanks in advance.
Avatar of jkna_gunn
jkna_gunn

try replacing \ with \\
Are you using "..." in your code.

e.g.

"$var" and expecting $var as a piece of text to appear somewhere rather than the value of the $var variable?

Can we see some code?
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you are using "..." (double quotes", then the content of the line will be evaluated first. This includes \, which is used to indicate an escape.

So, \$2.81 will probably go nuts.

Add ...

<?php
error_reporting(E_ALL);
?>

to the top of your code. Let's see ALL errors/warnings/notices.

Ideally, your code should produce NO errors/warnings/notices. Ideally. Sometimes you can't, but very rarely.

Avatar of dplinnane

ASKER

I'm using
$import_delim
$txt_qualifier



$fields    = fgetcsv($file_handle,1000,$import_delim,$txt_qualifier);
I'm using
$import_delim = '~';/tilde
$txt_qualifier   = '`';//backtick

$fields    = fgetcsv($file_handle,1000,$import_delim,$txt_qualifier);

I cannot modify the file (the user uploads the file) it is a fixed width file which I convert to cvs using the above delimeter and text qualifier. I thought the escape charater only escaped single and double quotes, if there are no double quotes or single quotes after the backslash what does it escape.

I have warning switched on I receive no warning for this poart of the code.  A starnge thing is that on my local dev machine,
win2k, apache the file is parsed properly it is only on linux that this problem occurs.

This is displayed in a html table I create. I have spaced out vals to show parsing.
JDS UNIPHASE CORP                     1/29/2003      SALE       \2,800.00\         $2.81         \"7,857.77\"       CUSIP: 46612J101
NORTEL NETWORKS CORP NEW     2/12/2003      SALE       \1,100.00\         $2.30         \"2,519.93\"       CUSIP: 656568102
The escape character can escape a LOT of things.

\t = tab
\n = newline
\r = carriage return
\b = backspace
\$ = $ literal when $ would need something else.

e.g.

$var = "a variable";
echo "\$var = \"$var\";";

will output

$var = "a variable";


Are you opening the file with in 't' mode?

Can you add ...

print_r($fields);

immediately after the $fields = fgetcsv(...);

line and cut'n'paste the results.

You may want to <pre>...</pre> the output first too.

Richard.
Value disappears in unix.
Opening file as follows.
    $file_handle    = fopen($upload_dir.$file_nm,"r");

Array Windows
(
    [0] => `JDS UNIPHASE CORP`
    [1] => `1/29/2003`
    [2] => `SALE`
    [3] => `\2,800.00\`
    [4] => `$2.81\"7,85`
    [5] => `7.77\"`
    [6] => `CUSIP: 46612J101`
 
)
 

Array Unix
(
    [0] => `JDS UNIPHASE CORP`
    [1] => `1/29/2003`
    [2] => `SALE`
    [3] => `\2,800.00\`
    [4] => `\"7,857.77\"`
    [5] => `CUSIP: 46612J101`
 
)

Try
 $file_handle    = fopen($upload_dir.$file_nm,"rt");

Can you put the file you are opening on the net somewhere please.

Thanks.
Me again. Why is it ALL my old questions are never awarded the points! This would have been easy to fix once I'd seen the file!