user3835015
user3835015

Reputation:

Use PHP to convert text file into array

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?

Upvotes: 5

Views: 28957

Answers (4)

Artur Kedzior
Artur Kedzior

Reputation: 4273

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.

Upvotes: 10

Shivam Sharma
Shivam Sharma

Reputation: 1607

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));

Upvotes: 0

user5707714
user5707714

Reputation: 1

<?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);
?>

Upvotes: 0

feeela
feeela

Reputation: 29942

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.

Upvotes: 1

Related Questions