Solved

fgetcsv and handling  values with a dollar sign ie $2.81

Posted on 2004-04-28
15
413 Views
Last Modified: 2013-12-13
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.
0
Comment
Question by:dplinnane
  • 8
  • 3
15 Comments
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10946973
try replacing \ with \\
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 10948245
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?
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
ID: 10948281
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.

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 10948283
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.

0
 

Author Comment

by:dplinnane
ID: 10948961
I'm using
$import_delim
$txt_qualifier



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

Author Comment

by:dplinnane
ID: 10949263
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:RQuadling
ID: 10950138
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.
0
 

Author Comment

by:dplinnane
ID: 10950587
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`
 
)

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 10957530
Try
 $file_handle    = fopen($upload_dir.$file_nm,"rt");

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 10957542
Can you put the file you are opening on the net somewhere please.

Thanks.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 11004631
Any joy?
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 11396874
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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now