Solved

fgetcsv and handling  values with a dollar sign ie $2.81

Posted on 2004-04-28
15
415 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:Richard Quadling
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:
Richard Quadling 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 40

Expert Comment

by:Richard Quadling
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
 
LVL 40

Expert Comment

by:Richard Quadling
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:Richard Quadling
ID: 10957530
Try
 $file_handle    = fopen($upload_dir.$file_nm,"rt");

0
 
LVL 40

Expert Comment

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

Thanks.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 11004631
Any joy?
0
 
LVL 40

Expert Comment

by:Richard Quadling
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

838 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