duanliao2310 2010-01-03 16:28 采纳率: 100%
浏览 180
已采纳

打开文本文件读取逗号分隔值

I have the following code:

$pollids  = "pollids.txt";
$contents = file_get_contents($pollids);
list($pollid) = explode(',', $contents);
echo $pollid;

This opens a text file containing a comma separated list of text: value1,value2,value3 etc...

However it only echo's the first piece of text in the file. How can I get it to loop/fetch them all?

Secondly, once I have these values, perhaps stored in an array, can i feed them into this piece of script?

$summize = new summize;
$search = $summize->search('searchterm');
$text = $search->results[0]->text;

So that ('searchterm') is replaced by each value in the file? Again i suspect some kind of loop within a loop?

  • 写回答

5条回答 默认 最新

  • dqdpz60048 2010-01-03 16:33
    关注

    Try this:

    $pollids  = "pollids.txt";
    $contents = file_get_contents($pollids);
    $pollfields = explode(',', $contents);
    
    echo $pollfields[0]; // Prints the value in first "cell"
    echo $pollfields[1]; // The second
    echo $pollfields[2]; // And so on
    

    Or, in a loop:

    foreach($pollfields as $field) {
        echo $field;
    }
    

    explode creates an array of fields separated by ,, so $pollfields is an array of those fields, and you can feed them to your second snippet like this:

    $summize = new summize;
    foreach($pollfields as $field) {
        $search = $summize->search($field);
    }
    $text = $search->results[0]->text;
    

    Without knowing more of how summize works, that should be what you need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了