• Re: Issue with extracting values from the string (initially from a file

    From He@21:1/5 to All on Wed Feb 2 02:23:32 2022
    Alla kirjutas LaupΓ€ev, 20. august 2016 kl 15:06:26 UTC+3:
    Hello!

    I will be grateful for your help on the issue of extracting values from a string, which I have obtained
    from a file.

    I have stored a file US.txt in my home directory from http://download.geonames.org/export/zip/readme.txt
    Here is the quote from the exercise I am doing: "US.txt is quite like a CSV file except that fields
    are delimited with \t (a tab character) instead of a comma. " Hence, I am using fgetcsv() function.

    Then i have tried to write a code to extract values from this file to be able to store them in a sql table.
    For now I am just testing the code by trying to extract only the first row from US.txt and get each
    variable, but I fail. var_dump() shows me that the whole string is stored at the key 0. I don't
    understand how to get each value separately.

    Please, take a look at this test code:

    <?php

    $file = "/home/ubuntu/workspace/US.txt";

    // perform tests on the file
    if(!file_exists($file))
    echo "Sorry, there is no such file $file\n";
    else if(!($open_file = fopen($file, "r")))
    echo "Sorry, couldn't open $file\n";
    else if(!is_readable($file))
    echo "$file is not readable\n";
    else if(filesize($file)==0)
    echo "Seems there is not content in the file $file\n";
    else
    {
    // store the first row of US.txt in a new variable
    $new_entry = fgetcsv($open_file, '\t');
    var_dump($new_entry);

    /******** var_dump result reveals all values of the first row stored as an array at key 0:
    array(1) {
    [0] =>
    string(48) "US 34034 APO AA Dillon 033 33.0364 -82.2493 1"
    }
    *********/

    //store each value in the string in a separate variable
    extract($new_entry);

    $entry1 = $new_entry[1]; // supposed to equal 'US'; it doesn't
    $entry2 = $new_entry[2]; // supposed to equal '34034'; it doesn't
    $entry3 = $new_entry[3]; // supposed to equal 'APO'; it doesn't
    $entry4 = $new_entry[4]; // supposed to equal 'AA'; it doesn't
    }


    Thank you very much!

    Hey

    Where are you ?

    πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)