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

  • SEARCH
  • Home
  • 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 8839749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:18:28+00:00 2026-06-14T10:18:28+00:00

I have a PHP application that is supposed to parse uploaded text files that

  • 0

I have a PHP application that is supposed to parse uploaded text files that has a format similar to this:

|                  |                |                  |
| -----------------------------------------------------|
| Sample           | Data           |                  |
| -----------------------------------------------------|
| Sample           | Data           |                  |
| -----------------------------------------------------|
| Sample           | Data           |                  |
| -----------------------------------------------------|


| Accepts                    |                            |
| --------------------------------------------------------|
| All                        | Yes                        |
| --------------------------------------------------------|
| More                       | Yes                        |
| --------------------------------------------------------|


|            |            | Years      |            |            |
| ---------------------------------------------------------------|
| 1998       | 1999       | 2000       | 2001       | 2002       |
| ---------------------------------------------------------------|
| 2003       | 2004       | 2005       | 2006       | 2007       |
| ---------------------------------------------------------------|
| 2008       | 2009       | 2010       | 2011       | 2012       |
| ---------------------------------------------------------------|

What I need to do is basically isolate each “block” by itself in the same order, so I can loop them one-by-one. A “solution” could be doing

preg_split("/\n{4,}/", $text);

However that would produce unwated results if the person submitting the text decides that the unnecessary newlines doesn’t belong and removes them. I tried playing around with preg_match_all(), but it has been years since I did any real regex, so I couldn’t come up with a usable solution.

The first line of a “block” always contains | and spaces, but fields may contain text. The last line of a “block” is always a pipe followed by a space, dashes to fill the row, ending with a |.

  • 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-14T10:18:29+00:00Added an answer on June 14, 2026 at 10:18 am

    If this is how the content of the text file looks like I would write something like

    $pat = '~
        (?<=^|\r{3}|\n{3}|(\r\n){3})    # beginning of string or following 3 newline chars
            \|[ ]                       # a pipe and a space
            (
                [ \S]+                      # 1 or more space or non space char
                \|                          # a pipe
            )+                          # 1 or more of this group
    
            (\n|\r\n?)                  # a newline
            \|[ ]-+\|                   # a pipe, a space, multiple dashes and a pipe
            (\n|\r\n?)                  # a newline
            .*?                         # anything between newlines above and below
            (\n|\r\n?)                  # a newline
            \|[ ]-+\|                   # a pipe, a space, multiple dashes and a pipe
        (?=$|\r{3}|\n{3}|(\r\n){3})     # end of string or followed by 3 newline chars
    ~sx';
    preg_match_all($pat,$str,$res);
    $blocks = $res[0];
    print_r($blocks);
    

    I’m not sure if this is the most elegant or even reliable way, though, since it’s hard to guess what exactly the content might look like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php application that uses the google feed api to get the
I have a PHP application that I have been having some problems with, some
I have a php application that is installed on several servers and all of
I have a PHP application that sends email using the pear Mail function. Unfortunately
I have a PHP application that is built around the MVC architecture without any
I have a php web application that uses big cookies to store a lot
We have a PHP/MYSQL application that collects user input, including special characters like ø,ü,ñ,
I have written a PHP application that uses Objects heavily. Added, deleting, updating etc..
I have a PHP based application that I need to work on both a
I am writing a PHP application that will have the ability to edit settings

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.