-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 "";
 ?>
2
  • 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. Commented May 27, 2012 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. Commented May 27, 2012 at 12:52

2 Answers 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 ~]$ 
Sign up to request clarification or add additional context in comments.

7 Comments

$streams = file('streams.txt'); didnt work
What did it do? In what way did it not work?
None of the streams showed up as online. Ive added all the code to the first post now.
Use print_r() or var_dump() to see what's inside $streams after you pull in its contents from the file.
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.
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.