read row and columns of text file in php

read row and columns of text file in php

php read file line by line
php read array from file
how to read csv file column wise in php
php file
php read csv file into associative array
how to read a particular column from csv file in php
php read csv file into array
php read csv file line by line

I have a text file the contents of which are as follows:

120MB 130MB 140MB 140MB
10% 20% 30% 40%

I have a php code which reads the file and prints the column.

<?php
$lines = file('test_ssh.txt');
foreach ($lines as $line) {
    $parts = explode(' ', $line);
    echo $parts[2] ;
}
?>

by this code I can read columns from the first row as and when required(i.e, if I need to popultae 1st column from 1st row in html table, I can do so by $parts[0])

but I am not able to read second line column by column(i.e. if I just need to read 1st column from 2nd row), I am not able to do it, I just get blank value.

Do I need some 2-D array to read lines and column separately or there is another easier way ? Please help with your expertise.


Not sure why you would only want to print one item from the first line and all the occurances from the second. But if this was really what you were asking how to do here is one way.

<?php
$lines = file('test_ssh.txt');
foreach ($lines as $lineNo => $line) {
    $parts = explode(' ', $line);
    if ( $lineNo < 1 ) {
        echo $parts[2] . PHP_EOL;
    } else {
        foreach( $parts as $col ) {  
            echo $col . ' ';
        }
    }
}

Result

140MB
10% 20% 30% 40%

After your comment below

Is this what you wanted

$lines = file('test_ssh.txt');
foreach ($lines as $line) {
    if ( $line == PHP_EOL ) { continue; } // avoid blank lines causing issues
    $columns = explode(' ', $line);
    echo '<tr>' . PHP_EOL;
    foreach( $columns as $col ) {  
        echo '<td>' . trim($col) . '</td>';
    }
    echo PHP_EOL . '</tr>' . PHP_EOL;
}

RESULT

<tr>
<td>120MB</td><td>130MB</td><td>140MB</td><td>140MB</td>
</tr>
<tr>
<td>10%</td><td>20%</td><td>30%</td><td>40%</td>
</tr>
<tr>
<td></td>
</tr>

ignore the PHP_EOL in the table output, they are there just to make the output look human readable.

Having reread the updated question, maybe this is what you want

function getColfromBothLines($col, $lines)
{
    $lin1 = explode(' ', $lines[0]);
    $lin2 = explode(' ', $lines[1]);
    return $lin1[$col] . ' - ' . $lin2[$col];
}

$lines = file('test_ssh.txt');


echo getColfromBothLines(0, $lines).PHP_EOL;
echo getColfromBothLines(1, $lines).PHP_EOL;
echo getColfromBothLines(2, $lines).PHP_EOL;

RESULT

120MB - 10%
130MB - 20%
140MB - 30%

fgetcsv - Manual, fgetcsv — Gets line from file pointer and parse for CSV fields It allows you to read rows from your CSV and get values based on the name of the column. PHP Read File - fread() The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read. The following PHP code reads the "webdictionary.txt" file to the end:


Try this! First foreach for lines and second for explodes.

        <?php
        $lines = file('test_ssh.txt');
        foreach ($lines as $line) {
            $parts = explode(' ', $line);
            foreach ($parts as $part) {
             echo $part;    
            }
        }

Do not close php tag :)

file - Manual, Note: If PHP is not properly recognizing the line endings when reading files either on or created by a read from CSV data (file) into an array with named keys In PHP, there is a function called fgetcsv, which will automatically parse CSV fields from a given resource handle (in this case, it will be the file handle resource of our file). In the following PHP example, I will read the CSV file in and then loop through each row using a while loop: PHP.


Try this.

$array = explode("\n", file_get_contents('file.txt'));

str_getcsv - Manual, fgetcsv() - Gets line from file pointer and parse for CSV fields line, this will create an array of associative arrays with the first row column headers as the keys. PHP and MySQL can read these files just as it can read a plain text file with data organized in rows and columns. So if you created an Excel spreadsheet with certain data and you want that data to be inserted into a MySQL database table, you simply save it with a .csv and then specify this file to be loaded into the MySQL table. MySQL & PHP, in the same way, can read plain text files that have been formatted in CSV format.


File-handling | PHP, We will take a look at how to open en close a file, how to read a file line by line, /*PHP program to open a csv file and print only the second column in the file. 3. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.


php tutorials: reading a text file into an array, PHP code to grab lines of text and place them into array. are in blue; the rest you have met before (get a file handle, loop round, use fgets to read the line). Parses a string input for fields in CSV format and returns an array containing the fields read.. Note: . The locale settings are taken into account by this function. If LC_CTYPE is e.g. en_US.UTF-8, strings in one-byte encodings may be read wrongly by this function.


How to to read lines from text file into a table format in PHP, [code]<?php $file = fopen("info.txt", "r") or die("Unable to open file!"); while (!feof($ file)){ $data = fgets($file); list($name, $number, $type, $country, $company,� For example, In testdata.txt file above, I want to skip first seven columns. I then want to take column 8 and multiply by 100 then divide by 3 and output result to output file. I want to do same for column 9. Column 10 I want to multiply by different number (by 3.5). thank you for the help! Maria