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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:22:49+00:00 2026-05-14T20:22:49+00:00

what is the best way of extracting multiple (~40 values) from a text file

  • 0

what is the best way of extracting multiple (~40 values) from a text file using php?

the data is more or less like:

NAMEA                   valuea
NAMEB                   valueb

I’m looking for a proper* approach to extracting this data into a data-structure, because i will need to specify regexs for all of them (all 40).

did i make myself clear?

*meaning, the default/painful method would be for me to do:

$namea = extractfunction("regexa", $textfilevalue);
$nameb = extractfunction("regeb", $textfilevalue);

… 40 times!


The lines may not be in the same order, or be present in each file. Every NAMEA is text like: “Registration Number:”, or “Applicant Name:” (ie, with spaces in what i was calling as NAMEA)


Response to the Col.

i’m looking for a sensible “way” of writing my code, so its readable, modifiable, builds an object/array thats easily callable, etc… “good coding style!” 🙂


@Adam – They do actually… and contain slashes as well…


@Alix – Freaking marvelous man! THat was GOOD! would you also happen to have any insights on how I can “truncate” the rsultant array by removing everything from “key_x” and beyond? Should i open that as a new question?

  • 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-14T20:22:50+00:00Added an answer on May 14, 2026 at 8:22 pm

    Here is my take at it:

    somefile.txt:

    NAMEA                   valuea
    NAMEB                   valueb
    

    PHP Code:

    $file = file_get_contents('./somefile.txt');
    $string = preg_replace('~^(.+?)\s+(.+?)$~m', '$1=$2', $file);
    $string = str_replace(array("\r\n", "\r", "\n"), '&', $string);
    
    $result = array();
    
    parse_str($string, $result);
    
    echo '<pre>';
    print_r($result);
    echo '</pre>';
    

    Output:

    Array
    (
        [NAMEA] => valuea
        [NAMEB] => valueb
    )
    

    You may also be able to further simplify this by using str_getcsv() on PHP 5.3+.


    EDIT: My previous version fails for keys that have spaces like @Col. Shrapnel noticed. I didn’t read the question with enough attention. A possible solution since you seem to be using keys that always have : appended is this:

    $string = preg_replace('~^(.+?):\s+(.+?)$~m', '$1=$2', $file);
    

    To remove everything from key_x to the end of the file you can do something like this:

    $string = substr($string, 0, strpos($string, 'key_x'));
    

    So the whole thing would look like this:

    somefile.txt:

    Registration Number:                   valuea
    Applicant Name:                   valueb
    

    PHP Code:

    $file = file_get_contents('./somefile.txt');
    $string = substr($file, 0, strpos($file, 'key_x'));
    $string = preg_replace('~^(.+?):\s+(.+?)$~m', '$1=$2', $string);
    $string = str_replace(array("\r\n", "\r", "\n"), '&', $string);
    
    $result = array();
    
    parse_str($string, $result);
    
    echo '<pre>';
    print_r($result);
    echo '</pre>';
    

    Output:

    Array
    (
        [Registration_Number] => valuea
        [Applicant_Name] => valueb
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Extracting text from HTML file using Python What is the best way
What is the best way of extracting the values from vvv.www.xxx.yyy.zzz Note that vvv
I am looking for the best way of extracting a 2D sub-matrix from a
What's the best way to go about extracting Outlook folders from within Delphi? Ideally
I'm looking into extracting all the content from a DTD using Perl, but I'm
I have a text file with something like Country1 city1 city2 Country2 city3 city4
What is the best way to obtain the class name for a requested PHP
I'm extracting samples from an audio file in order to draw the wave form
I am looking for a way to extract / scrape data from Word files
I Have a text file and want to parse it with PHP any one

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.