5

I have a text file here which I need to be able to convert into rows to extract the second, third, fourth, and fifth values from.

The first 7 values of each row are tab delimited, then there is a newline, then the final three values are tab delimited.

I removed the interrupting newlines so that each row is fully tab delimited.

<?php

$file="140724.txt";

$fopen = fopen($file, "r");

$fread = fread($fopen,filesize("$file"));

fclose($fopen);

$remove = "\n";

split = explode($remove, $fread);

foreach ($split as $string)
{
echo "$string<br><br>";
}

?>

Which produces this.

I'm not sure where to progress from this point. I'm teaching myself PHP and am still quite new to it, so I don't even know if where I've started from is a good place. My instinct is to write the previous output to a new textfile, then create another block of code similar to the first but exploding based on tabs, this time.

Help?

Malignus
  • 73
  • 1
  • 2
  • 6
  • 1
    possible duplicate of [How to create an array from a CSV file using PHP and the fgetcsv function](http://stackoverflow.com/questions/1269562/how-to-create-an-array-from-a-csv-file-using-php-and-the-fgetcsv-function) – feeela Jul 23 '14 at 09:01
  • Classic example of why you shouldnt just put links in a question or answer as both links above are dead. – Craicerjack Jan 13 '16 at 16:35

4 Answers4

10

You can process this file in one go like this:

<?php
    $file="140724.txt";

    $fopen = fopen($file, 'r');

    $fread = fread($fopen,filesize($file));

    fclose($fopen);

    $remove = "\n";

    $split = explode($remove, $fread);

    $array[] = null;
    $tab = "\t";

    foreach ($split as $string)
    {
        $row = explode($tab, $string);
        array_push($array,$row);
    }
    echo "<pre>";
    print_r($array);
    echo "</pre>";
?>

The result will be a jagged array:

enter image description here

You will need to clean up the 1st and the last element.

shaedrich
  • 5,457
  • 3
  • 26
  • 42
Artur Kedzior
  • 3,994
  • 1
  • 36
  • 58
  • 1
    I think $fopen = fopen($file, r); should be this --> $fopen = fopen($file, 'r'); r parameter as string otherwise it will throw a warning. good work. – Priyal Jun 11 '21 at 05:49
1

That is structured data, delimited by tabs. You can use fgetcsv() to read that data into an array. For an example see the PHP documentation.

feeela
  • 29,399
  • 7
  • 59
  • 71
  • Just as an additional, tab delimiter is \t so as the third parameter of fgetcsv put '\t' – peterpeterson Jul 23 '14 at 09:02
  • The original file has newlines in it, however, so it isn't purely tab delimited. Are you saying I should apply fgetcsv() to the output of my code block? – Malignus Jul 23 '14 at 09:03
  • 1
    @Malignus Are you kidding me? Have you even tried to open the PHP documentation? OK, next step – read it. – feeela Jul 23 '14 at 09:06
  • @feeela This is why it's a bad idea to try answering questions that the OP could have solved themselves with five minutes research :) – GordonM Jul 23 '14 at 09:10
  • @GordonM I'm willing to admit that I spent two hours trying to find the right way to do this before coming here. Like I said, I'm new, and am trying to teach myself this. The PHP Manual cannot be read in five minutes, and it's not easy to navigate to the precise function that's most useful for what you want. I tried delimiting purely by tabs, first, but I got unwanted rows because the newlines were interrupting the data. I'll keep reading, as I have been. Thanks for what help's been offered. – Malignus Jul 23 '14 at 09:14
0
<?php
$myfile = fopen("test.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
    $text[] = fgets($myfile);
}
fclose($myfile);
print_r($text);
?>
0

There is another answer here which converts file/raw strings into an associative array. It is really very handy in such cases.

function tab_to_array($src='', $delimiter=',', $is_file = true)
{
    if($is_file && (!file_exists($src) || !is_readable($src)))
        return FALSE;

    $header = NULL;
    $data = array();

    if($is_file){
        if (($handle = fopen($src, 'r')) !== FALSE)
        {
            while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
            {
                if(!$header)
                    $header = $row;
                else
                    $data[] = array_combine($header, $row);
            }
            fclose($handle);
        }
    }
    else{
        $strArr = explode("\n",$src);
        foreach($strArr as $dataRow){
            if($row = explode($delimiter,$dataRow))
            {
                if(!$header)
                    $header = $row;
                else
                    $data[] = array_combine($header, $row);
            }
        }
    }

    return $data;
}
/**
 * Example for file
 */
print_r(tab_to_array('example.csv'));
/**
 * Example for raw string
 */
$str = "name    number
Lorem   11
ipsum   22";
print_r(tab_to_array($str, "\t", false));
Shivam Sharma
  • 1,277
  • 15
  • 31