Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explode contents of a txt file into array

I´m having trouble exploding contents of a .txt file (structure below):

    01Name 1 
    02whatever contents
    03whatever contents
    -------------------
    01Name 2
    02whatever contents
    03whatever contents

As you can see, the "delimiter" is "-------------------". Now, the question is: how to explode this file into an array, so I can search for a specific name and display that block´s contents? I´ve tried to explode like this:

  header("Content-type:text/plain");
  $file = fopen("cc/cc.txt", "r");


  while (!feof($file)) {
    $lot = fgets($file);
    $chunk = explode("-------------------",$lot);

    print_r($chunk);

  }

  fclose($file);             

And got this as a result:

    Array
    (
        [0] => 01Name 1 

    )
    Array
    (
        [0] => 02whatever contents

    )
    Array
    (
        [0] => 03whatever contents

    )
    Array
    (
        [0] => -------------------

    )
    Array
    (
        [0] => 01Name 2

    )
    Array
    (
        [0] => 02whatever contents

    )
    Array
    (
        [0] => 03whatever contents
    )        

when i wanted to get this as a result:

    Array
    (
        [0] => 01Name 1
        [1] => 02whatever contents
        [2] => 03whatever contents

    )
    Array
    (
        [0] => 01Name 2
        [1] => 02whatever contents
        [2] => 03whatever contents
    )

I´ve searched PHP; assigning fgets() output to an array and Read each line of txt file to new array element , with no luck.

Any thoughts?

like image 292
MrsSammartino Avatar asked Oct 03 '12 16:10

MrsSammartino


People also ask

How do I get an array from a file?

In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method.

Why explode () is used?

The explode function is utilized to "Split a string into pieces of elements to form an array". The explode function in PHP enables us to break a string into smaller content with a break. This break is known as the delimiter.

Which code reads the entire contents of a file line by line into an array?

Accepted Answerstr = fileread('foo. txt');

What does the explode () function return?

The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.


1 Answers

You can use the following

$result = array();
$file = explode("-------------------", file_get_contents("cc/cc.txt"));
foreach ( $file as $content ) {
    $result[] = array_filter(array_map("trim", explode("\n", $content)));
}
var_dump($result);

Output

array
  0 => 
    array
      0 => string '01Name 1' (length=8)
      1 => string '02whatever contents' (length=19)
      2 => string '03whatever contents' (length=19)
  1 => 
    array
      1 => string '01Name 2' (length=8)
      2 => string '02whatever contents' (length=19)
      3 => string '03whatever contents' (length=19)

You can take it further

$result = array();
$file = explode("-------------------", file_get_contents("cc/cc.txt"));
foreach ( $file as $content ) 
{
    foreach(array_filter(array_map("trim",explode("\n", $content))) as $line)
    {
        list($key,$value) = explode(" ", $line);
        $result[$key] = $value ;
    }
}
var_dump($result);

Output

array
  '01Name' => string '2' (length=1)
  '02whatever' => string 'contents' (length=8)
  '03whatever' => string 'contents' (length=8)
like image 57
Baba Avatar answered Sep 25 '22 15:09

Baba