• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

I am having difficulty debugging this parsing code for a csv file

I'm playing around with a script to grab a csv file, parse the result and output it.  I'm not real good with PHP. right now the code outputs "Array" as the result of the function in it. Can anyone debug this and help me figure out what it's doing?
<?php
 
$array = array('MSFT','PH','PFE');
 
print getAllQuotes($array);
 
function getAllQuotes($symbolsarray) {
// returns an array with arrays with data
$allQuotes = array();
 
$baseurl = "http://quote.yahoo.com/download/quotes.csv?format=snlx&ext=.csv&symbols=";
// make the symbols urlencoded.
$urlsymbols = urlencode(implode(" ",$symbolsarray));
 
$quotesURL = $baseurl.$urlsymbols;
 
print $quotesURL;
 
$csv = file($quotesURL);
 
foreach ($csv as $line_num => $line) {
// if NOT exists format: "^AEX2","^AEX2", "N/A - <b>0.00</b>","N/A"
// if exists format: "^AEX" ,"AEX-Index","7:37am - <b>326.71</b>", "Amsterdam"
//"AHOc.BA","ROYAL AHOLD","Aug 28, 2002 - <b>16.435</b>","Buenos Aires"
//"MSFT.AS","MICROSOFT CORP","Oct 20 - <b>27.82</b>","Amsterdam"
// Please not the , in the date of AHOc.BA
// shame I don't know a better value for the 'format' in the baseurl. :-(
// split on ","
print $line_num;
 
$parts = explode("\",\"",$line);
$oneQuote = array();
$oneQuote["symbol"] = trim(trim($parts[0]) , "\"");
$oneQuote["name"] = trim(trim($parts[1]) , "\"");
$oneQuote["place"] = trim(trim($parts[3]) , "\"");
 
$val = trim($parts[2] , "\"");
 
// remove all before <b> and remove the last </b>
// remove 6:30am - <b>
$val = substr($val, strpos($val, "<b>")+3) ;
 
// remove </b>
$val = substr($val , 0 , -4) ;
 
$oneQuote["value"] = $val;
 
// add to allQuotes
$allQuotes[$oneQuote["symbol"]] = $oneQuote;
 
}
return $allQuotes;
 
}
 
?>

Open in new window

0
digitalwav
Asked:
digitalwav
  • 3
  • 3
1 Solution
 
Ray PaseurCommented:
Sorry - I can't follow all the details of your code, but try running the code snippet and looking at what prints.

Note especially what happens when you print an array vs when you print an element.  Note also the use of var_dump() to inspect the contents of a variable.  Useful in debugging!

HTH, ~Ray
<?php // RAY_print.php
 
echo "<pre>";
 
// AN ARRAY
$my_array = array('MSFT','PH','PFE');
print $my_array;
echo "\n\n"; // SHIM
 
 
 
// AN ITERATION OVER THE ARRAY
foreach ($my_array as $my_element) {
   print "$my_element \n";
}
echo "\n\n"; // SHIM
 
 
 
// A DUMP OF INFORMATION
var_dump($my_array);
echo "\n\n"; // SHIM
 
 
 
// A FORMATTED PRINT
print_r($my_array);
echo "\n\n"; // SHIM
 
 
 
// A FORMATTED PRINT OF THE LAST ELEMENT OF THE ARRAY
print_r($my_element);
 
?>

Open in new window

0
 
digitalwavIT Infrastructure ManagerAuthor Commented:
That will definitely help me figure it out.  the basic idea is to grab 3 bits of info from the csv file and output them.  Let me see what your debugging can do for me.
0
 
digitalwavIT Infrastructure ManagerAuthor Commented:
Ok, let me ask this....

It occurred to me that I need to download the csv file to parse it.  Does $csv = file($quotesURL); download it and open it into the $csv variable?
0
Independent Software Vendors: 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!

 
digitalwavIT Infrastructure ManagerAuthor Commented:
Got it!  I needed to do a print_r(getAllQuotes($array)); to get the array to output properly.  Thanks!
0
 
Ray PaseurCommented:
Have a look at this and its output.  I put comment bars with /* **** */ around the areas I modified.

It looks like you are getting the information into a useful format!

HTH, ~Ray
<?php // RAY_digitalwav.php
 
/* ******** */
echo "<pre>";
/* ******** */
 
$array = array('MSFT','PH','PFE');
 
/* ******** */
// print getAllQuotes($array);
print_r (getAllQuotes($array));
/* ******** */
 
 
function getAllQuotes($symbolsarray) {
// returns an array with arrays with data
$allQuotes = array();
 
$baseurl = "http://quote.yahoo.com/download/quotes.csv?format=snlx&ext=.csv&symbols=";
// make the symbols urlencoded.
$urlsymbols = urlencode(implode(" ",$symbolsarray));
 
$quotesURL = $baseurl.$urlsymbols;
 
print $quotesURL;
 
$csv = file($quotesURL);
 
 
/* ******** */
print_r($csv);
/* ******** */
 
foreach ($csv as $line_num => $line) {
// if NOT exists format: "^AEX2","^AEX2", "N/A - <b>0.00</b>","N/A"
// if exists format: "^AEX" ,"AEX-Index","7:37am - <b>326.71</b>", "Amsterdam"
//"AHOc.BA","ROYAL AHOLD","Aug 28, 2002 - <b>16.435</b>","Buenos Aires"
//"MSFT.AS","MICROSOFT CORP","Oct 20 - <b>27.82</b>","Amsterdam"
// Please not the , in the date of AHOc.BA
// shame I don't know a better value for the 'format' in the baseurl. :-(
// split on ","
print $line_num;
 
$parts = explode("\",\"",$line);
$oneQuote = array();
$oneQuote["symbol"] = trim(trim($parts[0]) , "\"");
$oneQuote["name"] = trim(trim($parts[1]) , "\"");
$oneQuote["place"] = trim(trim($parts[3]) , "\"");
 
$val = trim($parts[2] , "\"");
 
// remove all before <b> and remove the last </b>
// remove 6:30am - <b>
$val = substr($val, strpos($val, "<b>")+3) ;
 
// remove </b>
$val = substr($val , 0 , -4) ;
 
$oneQuote["value"] = $val;
 
// add to allQuotes
$allQuotes[$oneQuote["symbol"]] = $oneQuote;
 
}
return $allQuotes;
 
}
 
?>

Open in new window

0
 
Ray PaseurCommented:
Does $csv = file($quotesURL);

Yes.  See the man page here:
http://us3.php.net/manual/en/function.file.php
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now