Solved

fgetcsv and handling  values with a dollar sign ie $2.81

Posted on 2004-04-28
15
410 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
Comment Utility
try replacing \ with \\
0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I'm using
$import_delim
$txt_qualifier



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

Author Comment

by:dplinnane
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
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
Comment Utility
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
Comment Utility
Try
 $file_handle    = fopen($upload_dir.$file_nm,"rt");

0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
Can you put the file you are opening on the net somewhere please.

Thanks.
0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
Any joy?
0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
echo button 13 45
Re-imbursement Claim System 3 21
php documentation 4 19
How do I access property of nested object in PHP? 3 21
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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