dongyue7796 2012-05-27 11:53
浏览 51

php数组读取文本文件[重复]

Possible Duplicate:
Text file lines into array with PHP

How can I replace this

$streams = array(
"name1", 
"name2", 
"name3", 
"name4", 
"name5", 
"name6",
);

with something like

$streams = array(
streams.txt
);

where streams.txt includes

"name1", 
"name2", 
"name3", 
"name4", 
"name5", 
"name6",

Basically what the whole code does is that it reads from that array those names, and checks on justin.tv if the streams are online. Heres all of the code where I tried to fix it.

<?
$chan = "";
$streams = file('streams.txt');
echo "Live User Streams: ";
foreach ($streams as &$i) {
    $chan = "http://api.justin.tv/api/stream/list.json?channel=" . $i;
    $json = file_get_contents($chan);
    $exist = strpos($json, 'name');
    if($exist) {
        echo "$i  http://twitch.tv/" . $i . " | ";
    }

}
echo "";
 ?>
  • 写回答

2条回答 默认 最新

  • douxihui8270 2012-05-27 11:56
    关注

    The file() function seems to be what you want.

    $lines = file("streams.txt");
    

    You may have to massage your input text a little, or post-process the lines after they're sucked in to the array.

    UPDATE:

    Here's a command-line example that works on my workstation:

    [ghoti@pc ~]$ cat streams.txt 
    one
    two
    three
    [ghoti@pc ~]$ php -r '$a = file("streams.txt"); print_r($a);'
    Array
    (
        [0] => one
    
        [1] => two
    
        [2] => three
    
    )
    [ghoti@pc ~]$ 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100