tags:

views:

138

answers:

4

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 seperated 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 lopp/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?

+1  A: 

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.

Tatu Ulmanen
@Tatu In a CSV file, in order to have fields that contain commas, quotation marks are used to hold the field data, like this: `first value,"second, value",third value` if you used explode, you would get this: `array('first value','"second',' value"','third value')` which is not the desired result at all. `fgetcsv` is probably the direction you need to go with this answer to make it work.
Doug Neiner
explode won't work file_get_contents returns just one array element
streetparade
What the hell is this bashing about using `explode`? Bear in mind that that wasn't my idea, I just expanded on OP's own code that uses an `explode`. If I understood correctly, the question had *nothing* to do with how to open CSV file properly or how to return all rows of a file. We do not know what OP's source material looks like, it might be just one row of numbers separated by commas, in which case my answer is **perfectly valid**. "Comma separated" doesn't necessarily imply a CSV file. Thanks to the downvoters, though.
Tatu Ulmanen
@streetparade, file_get_contents doesn't return *any* array elements, it returns a string. What are you talking about?
Tatu Ulmanen
Tatu - Perfect solution to my problem, your correct the file is a text file with one row of numbers not a CSV file as such. In this case this is the answer. Oh and it works!
danit
+1  A: 

Use fgetcsv.

EDIT: explode won't work if values contain e.g. commas enclosed in quotes.

Mirko Nasato
What do escaped quotes have to do with `explode`'s ability to split the strings at commas?
Tatu Ulmanen
I meant commas in quotes, sorry.
Mirko Nasato
+4  A: 

Check out the function fgetcsv

This snippet of code is also from that page of documentation: (Example #1 Read and print the entire contents of a CSV file)

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>
Dominic Barnes
A: 
$pollids  = "pollids.txt";
$contents = file_get_contents($pollids);

$results = preg_split("/\s+|,/", $contents);
$summize = new summize;

for($i=0;$i<count($results);$i++)
{
echo $results[$i]:

$search[$i] = $summize->search($results[$i]);
$text[$i] = $search[$i]->results[$i]->text;
}

print_r($results);
print_r($search);
print_r($text);
streetparade