Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

fgetcsv and handling  values with a dollar sign ie $2.81

Posted on 2004-04-28
15
Medium Priority
?
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 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 …
Suggested Courses

610 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