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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:25:22+00:00 2026-06-08T15:25:22+00:00

I want to parse a file, and store it into an Array in PHP

  • 0

I want to parse a file, and store it into an Array in PHP. However, there are some rules which should the observed:

  • (p=”value”) should be ignored, but the "value" should be preserved.
  • - should be ignored.
  • whitespaces should be ignored.
  • split by \t and \n.

A sample string is :

NPD4196-2a_5_0
Geldanamycin - 0.166516 (p = 0.0068)    Alamethicin - 0.158302 (p = 0.0206)     4-Hydroxytamoxifen - 0.1429 (p = 0.0183)        Abietic acid - 0.133045 (p = 0.0203)    Caspofungin - 0.130885 (p = 0.0432)     Extract 00-303C - 0.12858 (p = 0.0356)  U73122 - 0.113274 (p = 0.0482)  Radicicol - 0.10213 (p = 0.0356)        Calcium ionophore - 0.096183 (p = 0.0262)

So, the goal is to produce a data structure like:

Array('NPD4196-2a_5_0' => Array(Array( 0 => 'Geldanamycin', 1 => '0.166516', 2 => '0.0068'), Array( ... ));

I have this written so far …

while(($line = fgets($fp)) !== false){
        $args = preg_split( '/[\t\n (=) ]+/', $line, -1, PREG_SPLIT_NO_EMPTY );
        if(count($args)){
           print_r($args);
           print "\n";
        }
}

What am I missing in other to accomplish my goal?
Thanks

  • 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-08T15:25:23+00:00Added an answer on June 8, 2026 at 3:25 pm

    This seems to work for one key-value pair (assuming NPD4196-2a_5_0 is the key in your example, and the second line is the value).

    <?php
    
    $fp = fopen('foo.txt', 'r');
    $regex = '/(\w*)\s*-\s*([\d\.]+)\s*\(p\s*=\s*([\d\.]+)\)/';
    $id = "NO ID";
    $result = Array();
    
    while(($line = fgets($fp)) !== false){
        if (!preg_match($regex, $line)) {
            $id = chop($line);
        } else {
            $all = Array();
    
            while (preg_match($regex, $line, $matches, PREG_OFFSET_CAPTURE)) {
                $last = end($matches);
                $line = substr($line, $last[1] + strlen($last[0]) + 1);
    
                $strings = Array();
    
                for ($i = 1; $i < 4; $i++) {
                    array_push($strings, $matches[$i][0]);
                }
    
                array_push($all, $strings);
            }
    
            $result[$id] = $all;
        }
    }
    
    print_r($result);
    ?>
    

    (That is a slightly edited version of David B’s regex.)

    If the line doesn’t match that long RegEx pattern, it will store the line as the ID. Otherwise, it will match the RegEx, then chop off the matching part. Each iteration of the inner while loop will match one entry. Since I am grabbing the indices of the matches, the for loop is used to only add the strings to the result.

    This prints:

    Array
    (
        [NPD4196-2a_5_0] => Array
            (
                [0] => Array
                    (
                        [0] => Geldanamycin
                        [1] => 0.166516
                        [2] => 0.0068
                    )
    
                [1] => Array
                    (
                        [0] => Alamethicin
                        [1] => 0.158302
                        [2] => 0.0206
                    )
    
                [2] => Array
                    (
                        [0] => Hydroxytamoxifen
                        [1] => 0.1429
                        [2] => 0.0183
                    )
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse xml file in utf-8 and sort it by some field.
I want to be able to parse file paths like this one: /var/www/index.(htm|html|php|shtml) into
Possible Duplicate: Parse a JavaScript file through PHP I have some pretty long string
I want to parse the xml file with dynamic content using DOM parser in
I want to parse a xml file using xquery in Ruby, I found this
I want to parse the html file, pdf file, csv file and text file.Now
I want to parse an input file that has syntax similar to c++ source.
I want to parse out each modified file that is reported during FCIV's verification
I want to parse a large XML file and I have two options: Perl
I want to parse a line from a CSV(comma separated) file, something like this:

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.