在 GAE 上使用 PHP 解析远程 csv 文件

发布于 2024-11-28 07:13:03 字数 310 浏览 1 评论 0原文

我似乎陷入了第 22 条军规,我正在使用 Quercus 在 Google App Engine 上用 PHP 开发一个小应用程序;

  1. 我有一个远程 csv 文件,我可以下载并下载它。存储在字符串中
  2. 要解析该字符串,我最好使用 str_getcsv,但 Quercus 还没有该函数
  3. Quercus 似乎确实知道 fgetcsv,但该函数需要一个我没有的文件句柄(而且我不能创建一个新的,因为 GAE 不允许创建文件)

有人知道如何解决这个问题,而不必放弃内置的 PHP csv 解析器函数并编写我自己的解析器吗?

I seem to be in a catch-22 with a small app I'm developing in PHP on Google App Engine using Quercus;

  1. I have a remote csv-file which I can download & store in a string
  2. To parse that string I'd ideally use str_getcsv, but Quercus doesn't have that function yet
  3. Quercus does seem to know fgetcsv, but that function expects a file handle which I don't have (and I can't make a new one as GAE doesn't allow files to be created)

Anyone got an idea of how to solve this without having to dismiss the built-in PHP csv-parser functions and write my own parser instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多孤肩上扛 2024-12-05 07:13:04

我认为最简单的解决方案确实是编写自己的解析器。无论如何,这都是小菜一碟,并且会让您学习更多正则表达式 - PHP 中没有用于数组解析器的 csv 字符串是没有意义的,因此完全有理由编写自己的正则表达式。只要确保它不会太慢即可;)

I think the simplest solution really is to write your own parser . it's a piece of cake anyway and will get you to learn more regex- it makes no sense that there is no csv string to array parser in PHP so it's totally justified to write your own. Just make sure it's not too slow ;)

一抹微笑 2024-12-05 07:13:04

您也许可以使用 stream_wrapper_register

以下是手册中读取全局变量的示例: http:// www.php.net/manual/en/stream.streamwrapper.example-1.php

然后您可以像普通文件句柄一样使用它:

$csvStr = '...';
$fp = fopen('var://csvStr', 'r+');
while ($row = fgetcsv($fp)) {
    // ...
}
fclose($fp);

You might be able to create a new stream wrapper using stream_wrapper_register.

Here's an example from the manual which reads global variables: http://www.php.net/manual/en/stream.streamwrapper.example-1.php

You could then use it like a normal file handle:

$csvStr = '...';
$fp = fopen('var://csvStr', 'r+');
while ($row = fgetcsv($fp)) {
    // ...
}
fclose($fp);
小清晰的声音 2024-12-05 07:13:04

这显示了我用带有限定、非限定、转义功能的示例输入编写的一个简单的手动解析器。它可用于标题行和数据行,并包含一个 assoc 数组函数,使您的数据成为 kvp 样式数组。

//example data
$fields = strparser('"first","second","third","fourth","fifth","sixth","seventh"');
print_r(makeAssocArray($fields, strparser('"asdf","bla\"1","bl,ah2","bl,ah\"3",123,34.234,"k;jsdfj ;alsjf;"')));


//do something like this
$fields = strparser(<csvfirstline>);
foreach ($lines as $line)
    $data = makeAssocArray($fields, strparser($line));


function strparser($string, $div = ",", $qual = "\"", $esc = "\\") {
    $buff = "";
    $data = array();
    $isQual = false; //the result will be a qualifier
    $inQual = false; //currently parseing inside qualifier

    //itereate through string each byte
    for ($i = 0; $i < strlen($string); $i++) {
        switch ($string[$i]) {
            case $esc:
                //add next byte to buffer and skip it
                $buff .= $string[$i+1];
                $i++;
                break;
            case $qual:
                //see if this is escaped qualifier
                if (!$inQual) {
                    $isQual = true;
                    $inQual = true;
                    break;
                } else {
                    $inQual = false; //done parseing qualifier
                    break;
                }
            case $div:
                if (!$inQual) {
                    $data[] = $buff;    //add value to data
                    $buff = "";         //reset buffer
                    break;
                }
            default:
                $buff .= $string[$i];
        }
    }
    //get last item as it doesnt have a divider
    $data[] = $buff;
    return $data;
}

function makeAssocArray($fields, $data) {
    foreach ($fields as $key => $field)
        $array[$field] = $data[$key];
    return $array;
}

this shows a simple manual parser i wrote with example input with qualifed, non-qualified, escape feature. it can be used for the header and data rows and included an assoc array function to make your data into a kvp style array.

//example data
$fields = strparser('"first","second","third","fourth","fifth","sixth","seventh"');
print_r(makeAssocArray($fields, strparser('"asdf","bla\"1","bl,ah2","bl,ah\"3",123,34.234,"k;jsdfj ;alsjf;"')));


//do something like this
$fields = strparser(<csvfirstline>);
foreach ($lines as $line)
    $data = makeAssocArray($fields, strparser($line));


function strparser($string, $div = ",", $qual = "\"", $esc = "\\") {
    $buff = "";
    $data = array();
    $isQual = false; //the result will be a qualifier
    $inQual = false; //currently parseing inside qualifier

    //itereate through string each byte
    for ($i = 0; $i < strlen($string); $i++) {
        switch ($string[$i]) {
            case $esc:
                //add next byte to buffer and skip it
                $buff .= $string[$i+1];
                $i++;
                break;
            case $qual:
                //see if this is escaped qualifier
                if (!$inQual) {
                    $isQual = true;
                    $inQual = true;
                    break;
                } else {
                    $inQual = false; //done parseing qualifier
                    break;
                }
            case $div:
                if (!$inQual) {
                    $data[] = $buff;    //add value to data
                    $buff = "";         //reset buffer
                    break;
                }
            default:
                $buff .= $string[$i];
        }
    }
    //get last item as it doesnt have a divider
    $data[] = $buff;
    return $data;
}

function makeAssocArray($fields, $data) {
    foreach ($fields as $key => $field)
        $array[$field] = $data[$key];
    return $array;
}
泪眸﹌ 2024-12-05 07:13:04

如果它可以又脏又快的话。我只想用
http://php.net/manual/en/function.exec.php
传递它并使用 sed 和 awk (http://shop.oreilly.com/product/9781565922259.do) 来解析它。我知道你想使用 php 解析器。我以前曾尝试过,但失败了,只是因为它没有直言不讳地表达自己的错误。
希望这有帮助。
祝你好运。

if it can be dirty and quick. I would just use the
http://php.net/manual/en/function.exec.php
to pass it in and use sed and awk (http://shop.oreilly.com/product/9781565922259.do) to parse it. I know you wanted to use the php parser. I've tried before and failed simply because its not vocal about its errors.
Hope this helps.
Good luck.

2024-12-05 07:13:04

您也许可以将 fopenphp://tempphp://memory (php.net) 使其正常工作。您要做的就是打开 php://tempphp://memory,写入内容,然后倒带(php.net),然后将其传递给 fgetcsv。我没有测试这个,但它可能有效。

You might be able to use fopen with php://temp or php://memory (php.net) to get it to work. What you would do is open either php://temp or php://memory, write to it, then rewind it (php.net), and then pass it to fgetcsv. I didn't test this, but it might work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文