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?

2

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

5 Comments

Just as an additional, tab delimiter is \t so as the third parameter of fgetcsv put '\t'
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 Are you kidding me? Have you even tried to open the PHP documentation? OK, next step – read it.
@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 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.
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);
?>

1 Comment

A little explanation of what your code is doing wouldn't hurt anyone.
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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.