0

the csv data as the following:

  (first line)   price   url

  (second line)    $10   src="http://www.test.com/1455/"||src="http://www.test.com/image/1456/"||

  (third line)    $20  src="http://www.test.com/2260/"||src="http://www.test.com/2261/"||

      ....   ........

now i want to put all the data from the url column into a text file. and shows the data one by one. namely:

http://www.test.com/1455/
http://www.test.com/image/1456/
http://www.test.com/2260/
http://www.test.com/2261/
....

now,i am stucked.

a, i don't know how to prevent outputting the first line. b,i don't know how to get the url, and put them i a text file one by one.

first i using the following code to output the data to the screen.

$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);

        $row++;

       echo $data[1] . "<br />\n";

    }
    fclose($handle);
}

the above code also output the first line (url). i don't want to output it. how should i do. thank you.

4

2 回答 2

0

你很亲近!我已经稍微修改了你的代码来做你想做的事情。本质上,您需要第二个文件处理程序来处理输出。您可以将其作为附加模式打开,以便将写入附加到文件末尾。至于不回显标题,您只需确保该行大于第一行。希望能帮助到你!

    $row = 1;
    if (($handle = fopen("test.csv", "r")) !== FALSE && ($handle2 = fopen("output.txt", 'a')) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if($row > 1) {
                fwrite($handle2, "{$data[1]}\n");
            }
            $row++;
        }
        fclose($handle);
        fclose($handle2);
    }
于 2012-08-23T05:10:08.917 回答
0

// 跳过 CSV 的第一行 - 输出数据的第二“列”。

$row = 0;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        if ($row > 0 ) {
          echo $data[1] . "<br />\n";
        }
        $row++;
    }
    fclose($handle);
}
于 2012-08-23T05:30:38.700 回答