0

Okay, i've read a lot of information on parsing a .TXT file. but I cant seem to work this one out.Ultimately i need to parse the data from a .txt file into an array starting after a specific line that says '!data;' and stopping at '!enddata;' below is the example of my source file. I want to pick out all of the data separated by the colon and place it into an array. if you can give me some asssistance; or point me in the right direction, that would be awesome :)

Usless text
!data;
data1:data2:data3:data4:data5:data6:data7:
data1:data2:data3:data4:data5:data6:data7:

!enddata;
More Useless text

i know on each line i need to use the explode function, i just dont know how to get it to start stop at that specific line!

4 Answers 4

1

Regular expressions aren't necessary. Use file to split a file into an array of lines, and create a boolean value (here, $capture) to "remember" whether future lines should be captured or not.

$lines = file('example.txt', FILE_IGNORE_NEW_LINES);
$start_identifier = '!data;';
$end_identifier = '!enddata;';
$capture = false;
$elements = array();

foreach($lines as $line){
    // Capture subsequent lines, skip this line which doesn't contain data
    if($line == $start_identifier){
        $capture = true;
        continue;
    }
    // Skip subsequent lines, skip this line which doesn't contain data
    elseif($line == $end_identifier){
        $capture = false;
        continue;
    }

    // If we should capture this line, implement splitting logic to do so
    if($capture and !empty($line)){
        // Split elements of this line
        $line_elements = explode(':', $line);
        // Remove empty elements
        $line_elements = array_filter($line_elements, function($e){ return !empty($e); });
        // Add this line's elements to array of all elements
        $elements = array_merge($elements, $line_elements);
    }
}

$elements; // array('data1', 'data2', ...
Sign up to request clarification or add additional context in comments.

1 Comment

This answer helped me the most and modeled (in a cleaner way) the path that i was going down. Thanks!
1

You could do like this using array_slice and file in PHP

<?php
echo "<pre>";
$arr=file('status.txt'); //<---- Grabbing all the content to an array 
$arr = array_map('trim',$arr);
$keyStart = array_search(trim('!data;'),$arr);   //<----- Beginning Key
$keyEnd=array_search('!enddata;',$arr);          //<----- Ending key
$output = array_slice($arr, $keyStart, $keyEnd); // Use slice to extract portion !
print_r($output);                                // Print your results ! 

OUTPUT :

Array
(
    [0] => !data;
    [1] => data1:data2:data3:data4:data5:data6:data7:
    [2] => data1:data2:data3:data4:data5:data6:data7:
    [3] => 
    [4] => !enddata;
)

1 Comment

Great Answer Shankar!
0

I'm going to answer this with regex.

Our plan of attack:

  1. strip everything before '!data;' and everything after '!enddata;'
  2. remove all newline characters
  3. explode on ':'

    $file = file_get_contents('data.txt');
    $file = preg_replace('/.*!data;/ms', '', $file);
    $file = preg_replace('/!enddata;.*$/ms', '', $file);
    
    $file = preg_replace('/\r\n/', '', $file);
    
    $result = explode(':', $file);
    

Explanations: The m modifier lets it match on multiline strings.

The s modifier (dotall) lets . match newline characters

\r\n handles windows type newlines. You can change that to just \n on linux (or do both types to handle both types)

Finally, explode splits on ':' character.

Comments

0
$arr = array();
$temp ="";
$i=0;
$handle = @fopen("text.txt", "r");
if ($handle) {
        while (($buffer = fgets($handle, 4096)) !== false) {
            $temp = $temp . $buffer;
            if(strpos($temp,"!enddata;") === false){
            if (strpos($temp,'!data;') !== false) {
               $arr[$i] =  $buffer;
            }
            $i++;
        }   
    }
    fclose($handle);
    echo "<pre>";print_r($arr);
}

1 Comment

while(strpos($temp,'!enddata') !== true) is an infinite loop. strpos never returns true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.