/** * Analog of {@link json_encode()} for .properties keys/values. */ function csv_encode($s) { if (is_array($s)) { $output = ""; foreach ($s as $value) { $output .= ($output ? "," : "") . csv_encode($value); } return $output . "\n"; } else { return "\"" . str_replace("\"", "\"\"", $s) . "\""; } }
function my_content_type_exception_handler($e) { $message = "Error: " . htmlspecialchars($e->getMessage()); echo csv_encode("error") . "," . csv_encode($e->getMessage()) . "\n"; }
function displayResultCsv($app, $result) { $app->response->headers->set('Content-Type', 'text/csv'); $app->response->headers->set('Content-Disposition', 'attachment; filename="bidtorrent-stats.csv"'); echo csv_encode($result); }
if ($team1) { // $record is a new array to store the values found $record = array('team1' => $team1[0]->innertext, 'team2' => $team2->innertext, 'comp' => $comp[0]->innertext, 'time' => $time[0]->innertext, 'chan' => $chan[0]->innertext, 'date' => $date[0]->last_child(1)->innnertext); // output json print csv_encode($record) . "\n"; scraperwiki::save(array('team1', 'team2', 'comp', 'time', 'chan', 'date'), $record); } } $html = scraperWiki::scrape("http://walkabout.eu.com/sport/"); // print $html . "\n"; require 'scraperwiki/simple_html_dom.php'; $dom = new simple_html_dom(); $dom->load($html); // for each line of html find td foreach ($dom->find("div[class=sportsrow]") as $data) { // print $data; $team1 = $data->find("div[class=sport_teams] strong"); $team2 = $data->find("div[class=sport_teams] strong", 1); $date = $data->find("div[class=start_time]"); $time = $data->find("div[class=start_time] strong"); $comp = $data->find("div[class=competition_name] strong"); $sport = $data->find("div[class=sportrow]"); $chan = $data->find("div[class=channel_name]"); if ($team1) { // $record is a new array to store the values found $record = array('team1' => $team1[0]->innertext, 'team2' => $team2->innertext, 'comp' => $comp[0]->innertext, 'time' => $time[0]->innertext, 'chan' => $chan[0]->innertext, 'date' => $date[0]->last_child(1)->innnertext); // output json print csv_encode($record) . "\n"; scraperwiki::save(array('team1', 'team2', 'comp', 'time', 'chan', 'date'), $record); } }
//get area if (!$info_only) { $response['area'] = get_area(); } } $response['header']['code'] = 0; //put some error handling in later $end_time = get_microseconds(); $response['header']['time'] = round($end_time - $start_time, 4); if ($download) { header('Pragma: no-cache'); switch ($download) { case 'json': header('Content-type: application/json'); header('Content-Disposition: attachment; filename=verifier-search.json'); echo json_encode(array('codes' => $response['codes'])); break; case 'excel': excel_encode($response); //xls function echos inline break; case 'csv': header('Content-type: application/csv'); header('Content-Disposition: attachment; filename=verifier-search.csv'); csv_encode($response); break; } } else { header('Content-type: application/json'); echo json_encode($response); }
$header[] = "Amount 2 Daily Rate Bid"; } echo csv_encode($header); foreach ($transactions as $transaction) { $row = array($transaction['id'], date("Y-m-d", strtotime($transaction['transaction_date'])), $transaction['exchange'] . "-" . $transaction['account_id'], $transaction['exchange_name'], $transaction['account_title'], isset($finance_categories[$transaction['category_id']]) ? $finance_categories[$transaction['category_id']]['title'] : "", $transaction['is_automatic'] ? "(generated automatically)" : $transaction['description'], $transaction['is_automatic'] ? $transaction['id'] : $transaction['reference'], $transaction['value1'], $transaction['currency1'], $transaction['value2'], $transaction['currency2']); if ($page_args['include_rates']) { $row[] = isset($transaction['rates1_exchange']) ? $transaction['rates1_exchange'] : ""; $row[] = isset($transaction['rates1_currency1']) ? $transaction['rates1_currency1'] : ""; $row[] = isset($transaction['rates1_currency2']) ? $transaction['rates1_currency2'] : ""; $row[] = isset($transaction['rates1_bid']) ? $transaction['rates1_bid'] : ""; $row[] = isset($transaction['rates2_exchange']) ? $transaction['rates2_exchange'] : ""; $row[] = isset($transaction['rates2_currency1']) ? $transaction['rates2_currency1'] : ""; $row[] = isset($transaction['rates2_currency2']) ? $transaction['rates2_currency2'] : ""; $row[] = isset($transaction['rates2_bid']) ? $transaction['rates2_bid'] : ""; } echo csv_encode($row); } performance_metrics_page_end(); return; } } page_header(t("Your Transactions"), "page_your_transactions", array('js' => array('accounts', 'transactions'), 'class' => 'report_page page_finance')); ?> <!-- page list --> <?php $page_id = -1; $your_transactions = true; require __DIR__ . "/_finance_pages.php"; ?>
function testCSVEncode() { $this->assertEquals("\"hello\"", csv_encode("hello")); $this->assertEquals("\"hello\"\"\"", csv_encode("hello\"")); $this->assertEquals("\"hello, world\"", csv_encode("hello, world")); }