-4

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 "";
 ?>
Community
  • 1
  • 1
qwerty1911
  • 47
  • 1
  • 8
  • Is the text file something you generate yourself? If so, don't try to make it "half an array". Just have the plain names one on each row (without the quotes and commas) and read them into an array. – JJJ May 27 '12 at 11:57
  • Per your edit, if this is how you're using each line in streams.txt, and streams.txt looks like what you posted above, then it's pretty obvious why this is failing. Clean up streams.txt, and do some debugging -- use `print_r()` to see what's in `$streams`, and echo `$chat` to see what you're actually trying to fetch. Oh, and don't use [short tags](http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use). – ghoti May 27 '12 at 12:52

2 Answers2

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 ~]$ 
ghoti
  • 45,319
  • 8
  • 65
  • 104
  • $streams = file('streams.txt'); didnt work – qwerty1911 May 27 '12 at 12:43
  • What did it do? In what way did it not work? – ghoti May 27 '12 at 12:44
  • None of the streams showed up as online. Ive added all the code to the first post now. – qwerty1911 May 27 '12 at 12:45
  • Use `print_r()` or `var_dump()` to see what's inside `$streams` after you pull in its contents from the file. – ghoti May 27 '12 at 12:47
  • Nope dosent work for me. Also my goal is to never have to touch the .php file again, just add to the .txt file new streamers. – qwerty1911 May 27 '12 at 12:53
  • @user1172196 `file()` is what you want, I'm willing to bet the problem is that you didn't `trim()` the line before using it in an API query. `file()` also returns the newline on the end of each line, if you pass it through `trim()` before using it in the API call it will strip this off and not mess up the URL – DaveRandom May 27 '12 at 12:56
  • Well, I guess this is where I'm unable to help further. If you can't produce debugging information, there's no way to determine WHY it doesn't work. Obviously, the PHP functions *do* work. The problem is just in your code. – ghoti May 27 '12 at 12:56
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.

qwertyboy
  • 1,576
  • 15
  • 25