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 6703839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:13:09+00:00 2026-05-26T07:13:09+00:00

I would like to scan a large piece of text using PHP and find

  • 0

I would like to scan a large piece of text using PHP and find all matches for a pattern, but then also 2 lines above the match and 2 lines below.

My text looks like this, but with some extra unnecessary text above and below this sample:

1

Description text

123.456.12

10.00

10.00

3

Different Description text

234.567.89

10.00

30.00

#Some footer text that is not needed and will change for each text file#

15

More description text

564.238.02

4.00

60.00

15

More description text

564.238.02

4.00

60.00

#Some footer text that is not needed and will change for each text file#

15

More description text

564.238.02

4.00

60.00

15

More description text

564.238.02

4.00

60.00

Using PHP, I am looking to match each number in bold (always same format – 3 numbers, dot, 3 numbers, dot, 2 numbers) but then also return the previous 2 lines and the next 2 lines and hopefully return an array so that I can use:

$contents[$i]["qty"] = "1";
$contents[$i]["description"] = "Description text";
$contents[$i]["price"] = "10.00";
$contents[$i]["total"] = "10.00";

etc…

Is this possible and would I use regex? Any help or advice would be greatly appreciated!

Thanks

ANSWERED BY vzwick

This is my final code that I used:

$items_array = array();
$counter = 0;

if (preg_match_all('/(\d+)\n\n(\w.*)\n\n(\d{3}\.\d{3}\.\d{2})\n\n(\d.*)\n\n(\d.*)/', $text_file, $matches)) {

    $items_string = $matches[0];
    foreach ($items_string as $value){

        $item = explode("\n\n", $value);

        $items_array[$counter]["qty"] = $item[0];
        $items_array[$counter]["description"] = $item[1];
        $items_array[$counter]["number"] = $item[2];
        $items_array[$counter]["price"] = $item[3];
        $items_array[$counter]["total"] = $item[4];

        $counter++;

    }

}
else
{
    die("No matching patterns found");
}

print_r($items_array);
  • 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-05-26T07:13:09+00:00Added an answer on May 26, 2026 at 7:13 am
    $filename = "yourfile.txt";
    $fp = @fopen($filename, "r");
    if (!$fp) die('Could not open file ' . $filename);
    
    $i = 0; // element counter
    $n = 0; // inner element counter
    
    $field_names = array('qty', 'description', 'some_number', 'price', 'total');
    $result_arr = array();
    
    while (($line = fgets($fp)) !== false) {
        $result_arr[$i][$field_names[$n]] = trim($line);
        $n++;
        if ($n % count($field_names) == 0) {
            $i++;
            $n = 0;
        }
    }
    
    fclose($fp);
    print_r($result_arr);
    

    Edit: Well, regex then.

    $filename = "yourfile.txt";
    $file_contents = @file_get_contents($filename);
    if (!$file_contents) die("Could not open file " . $filename . " or empty file");
    if (preg_match_all('/(\d+)\n\n(\w.*)\n\n(\d{3}\.\d{3}\.\d{2})\n\n(\d.*)\n\n(\d.*)/', $file_contents, $matches)) {
        print_r($matches[0]);
        // do your matching to field names from here ..
    }
    else
    {
        die("No matching patterns found");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if there is any way to scan a text
I would like to scan a directory for any assemblies that are not already
I have a directory of files that I would like to scan on a
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to sort an array in ascending order using C/C++ . The
I would like to scan paired bluetooth devices to look for services before perform
I have been seeing many apps on appstore concerning finger scan. I would like
What i would like to do is scan a disc or a drive (usb,
I would like to scan a directory that has image files and populate my
The goal is that I would like to cycle through ALL of the .CSV

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.