Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:05:57+00:00 2026-06-12T08:05:57+00:00

I´m having trouble exploding contents of a .txt file (structure below): 01Name 1 02whatever

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:05:58+00:00Added an answer on June 12, 2026 at 8:05 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble with an .htaccess file on WAMP. Works on the live server, but
I'm exploring mod_python and I'm having trouble with the package importing. I've a structure
Having trouble determining if a file name conforms to a specific convention as follows.
Having trouble rewriting the name of a flash file: /flash/shell.1257347618.swf (the numbers are a
Having trouble using foreach to output the contents of a mysql table. The table
having trouble coming up with search parameters for this issue, so I can't find
Having trouble getting this to apply in MySQL Workbench 5.2.15 DELIMITER // CREATE DEFINER=`potts`@`%`
Been having some trouble implementing a single-column index in LaTeX. I've been employing the
Having trouble with each function... Will try to explain by example... In my code,
Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.