Spilt a Text file and assign

Discussion in 'PHP' started by kolucoms6, Aug 30, 2014.

  1. #1
    I have a text file as

    Name1,9999999,123
    Name2,8888888,456
    Name3,7777777,426

    I want to create sentences in PHP like
    Mr Name1 , your number is 9999999 and value is 123.
    Mr Name2 , your number is 8888888 and value is 426.

    How to do it using PHP ?

    I was trying :

    $row = 1;
    $handle = fopen("data.txt", "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    $row++;
    
    for ($c=0; $c < $num; $c++)
    {
    $Name = $data[$c] . "<br />\n";
    $key = $data[$c];
    }
    
    }
    fclose($handle);
    
    
    
    Code (markup):
    But couldnt.

    Please help.
     
    kolucoms6, Aug 30, 2014 IP
  2. funzie

    funzie Well-Known Member

    Messages:
    71
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    123
    #2
    First understand what this code does. If you look at the $data array for each line you will get a value of
    
    Array
    (
        [0] => Name1
        [1] => 9999999
        [2] => 123
    )
    
    Code (markup):
    For the first row, for example.

    So you have three elements there. Your for loop is looping through all three of them. So anything you print will be duplicated three times. Since you already know what the number of elements is. The easiest way is to call each array element. For example:
    
    print "Mr $data[0] , your number is $data[1] and value is $data[2].\n";
    
    Code (markup):
    No need for the internal for loop.
     
    funzie, Aug 31, 2014 IP
  3. donjajo

    donjajo Active Member

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    51
    #3
    This might be an easy way :)

    <?php
    $data = 'Name1,9999999,123
    Name2,8888888,456
    Name3,7777777,426';
    $data = explode ( "\n", $data );
    foreach ( $data as $d ) {
       $dd[] = explode ( ',', $d );
    }
    print_r ( $dd );
    ?>
    PHP:
    and output is
    Array
    (
        [0] => Array
            (
                [0] => Name1
                [1] => 9999999
                [2] => 123
            )
    
        [1] => Array
            (
                [0] => Name2
                [1] => 8888888
                [2] => 456
            )
    
        [2] => Array
            (
                [0] => Name3
                [1] => 7777777
                [2] => 426
            )
    
    )
    
    Code (markup):
    Find a way and work around it then :)
     
    donjajo, Sep 1, 2014 IP
  4. funzie

    funzie Well-Known Member

    Messages:
    71
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    123
    #4
    Using explode works, but not really the optimal way. It's like using a wrench to get out a screw. Fgetcsv is a built in function to read CSV files. So you would want to use that as it is made to read CSV files.
     
    funzie, Sep 1, 2014 IP
  5. Krellen

    Krellen Greenhorn

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #5
    Lots of ways to do this, but this is probably the easiest:

    <?php
    $handle = fopen('test.txt', 'r');
    
    while ($line = fgetcsv($handle)) {
        echo 'Mr ' . $line[0] . ', your number is ' . $line[1] . ' and value is ' . $line[2] . '.';
    }
    Code (markup):
     
    Krellen, Sep 10, 2014 IP
  6. pmf123

    pmf123 Notable Member

    Messages:
    1,449
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    215
    #6
    how often does your file change? you should import it into mysql
     
    pmf123, Sep 15, 2014 IP
  7. Vibrantu

    Vibrantu Greenhorn

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #7
    You can read file line by line and use explode function to convert line into array and then use implode function to combine again in your required format
     
    Vibrantu, Sep 21, 2014 IP