-4

可能重复:
使用 PHP 将文本文件行放入数组

我该如何替换这个

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

有类似的东西

$streams = array(
streams.txt
);

其中streams.txt 包括

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

基本上,整个代码的作用是从该数组中读取这些名称,并检查 justin.tv 是否在线。这是我试图修复它的所有代码。

<?
$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 "";
 ?>
4

2 回答 2

2

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 ~]$ 
于 2012-05-27T11:56:47.553 回答
0

You need to read the file's content (you can use file_get_content), and then use explode to turn it into an array.

于 2012-05-27T12:00:44.010 回答