导入.csv为值提供\ x0P \ x0a \ x0g \ x0e \ x0

时间:2014-02-28 16:43:03

标签: php csv

我正在尝试导入由Google Adsense导出为CSV按钮生成的.csv文件,但我遇到了我认为编码问题。

下载的.csv文件的内容是:

Date    Page views  Clicks  Page CTR    CPC (USD)   Page RPM (USD)  Estimated earnings (USD)
2014-02-20  381480  2673    0.70%   0.23    1.61    615.40
2014-02-21  304515  2206    0.72%   0.21    1.52    463.32
2014-02-22  192661  1491    0.77%   0.21    1.59    307.19
2014-02-23  230952  1706    0.74%   0.23    1.70    392.45
2014-02-24  280726  1817    0.65%   0.20    1.31    367.04
2014-02-25  279398  1889    0.68%   0.21    1.40    390.49
2014-02-26  292201  2045    0.70%   0.22    1.55    454.26

当输出到浏览器或在Notepad ++等文本编辑器中查看时,字符会按预期显示(如上所示)。但是,当我在IDE中逐步执行它时,我将获得以下作为第一行的数组:

array = 
  0: string = "\x02\x00\x01\x04\x0-\x00\x02\x0-\x02\x00\x0"
  1: string = "\x03\x08\x01\x04\x08\x00\x0"
  2: string = "\x02\x06\x07\x03\x0"
  3: string = "\x00\x0.\x07\x00\x0%\x0"
  4: string = "\x00\x0.\x02\x03\x0"
  5: string = "\x01\x0.\x06\x01\x0"
  6: string = "\x06\x01\x05\x0.\x04\x00\x0"

我几乎每次尝试过编码转换时都会想到,但似乎没有什么能理解这些十六进制字符。

这是我运行的脚本将其转换为数组,但是,只要我使用file或file_get_contents,就会看到这些字符。

$csv = array();
$lines = file($path, FILE_IGNORE_NEW_LINES);

foreach ($lines as $key => $value)
{              
    $row = str_getcsv($value, "\t");

    $csv[$key]['Date'] = $row[0];
    $csv[$key]['Site'] = $row[1];
    $csv[$key]['Pageviews'] = $row[2];
    $csv[$key]['Clicks'] = $row[3];
    $csv[$key]['PageCTR'] = str_replace('%', '', $row[4]);
    $csv[$key]['CPC'] = $row[5];
    $csv[$key]['PageRPM'] = $row[6];
    $csv[$key]['Earnings'] = $row[7];    
}      

echo '<pre>';
print_r($csv);
echo '</pre>';

如何将\x02\x06\x07\x03\x0变成2673?

0 个答案:

没有答案