Jump to content

Variable names


timmah1

Recommended Posts

How can I find out the variable names from this csv?

 

Here is what I have, it displays all the content with no problem, but I would like to be to know information is what

Example

name = GOOG

 

I was looking around and found preg_match, to find out everything, but I have no idea how I would use it.

<?php
$row = 1;
$handle = fopen("http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=saa2a5bb6b4ncc1c3dd2d1eghs1e7xt6f6l2l3e1g4g1g3opk3ll1j1ij4p2p5mqrn4s7r5p1t7t1p6v1t8r1w1yvkjj5j6k4k5m3m7m8m4m6m5we9r6r7e8c8g6k2m2r2b2j3v724b3c6g5i5k1", "r");

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 " $name" . $data[$c] . "<br />\n";

    }
}
fclose($handle);
?>

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

Need more information about the CSV. Are there name/value pairs on each line or what. If the name/value pairs are on each line such as:

name1=value1
name2=value2
name3=value3

 

Then that is a simple procedure:

$data = file('path/to/csvfile.csv');
foreach ($data as $line)
{
    $nameValue = explode('=', $line);
    $name = trim($nameValue[0]);
    $value = trim($nameValue[1]);
    echo "Variable: $name, Value: $value<br />";
}

//Output
//Variable: name1, Value: value1
//Variable: name2, Value: value2
//Variable: name3, Value: value3

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.