0

I have some text like this: (This file contains some block of text file, that starts with the line : x/x/x/x and ends with tgt-noise-margin-down : x phrase)

------------------------------------------------------------------------------------------------------------------------------------
linkup-record
------------------------------------------------------------------------------------------------------------------------------------
line : 1/1/6/31              link-timestamp-down : 2014-06-16:00:06:11
attained-bitrate-down : 7616                       max-bitrate-up : 1024                     max-bitrate-down : 4096
threshold-bitrate-up : 512                threshold-bitrate-down : 1024                         max-delay-up : 15
max-delay-down : 15                    tgt-noise-margin-up : 60                  tgt-noise-margin-down : 60 
------------------------------------------------------------------------------------------------------------------------------------
linkup-record
------------------------------------------------------------------------------------------------------------------------------------
line : 1/1/6/32              link-timestamp-down : 2014-06-16:00:06:11
attained-bitrate-down : 3616                       max-bitrate-up : 512                     max-bitrate-down : 2048
threshold-bitrate-up : 256                threshold-bitrate-down : 512                         max-delay-up : 15
max-delay-down : 15                    tgt-noise-margin-up : 60                  tgt-noise-margin-down : 60 
------------------------------------------------------------------------------------------------------------------------------------
linkup-record
------------------------------------------------------------------------------------------------------------------------------------
line : 1/1/6/33              link-timestamp-down : 2014-06-16:00:06:11
attained-bitrate-down : 4096                       max-bitrate-up : 1024                     max-bitrate-down : 4096
threshold-bitrate-up : 1024                threshold-bitrate-down : 1024                         max-delay-up : 15
max-delay-down : 15                    tgt-noise-margin-up : 100                  tgt-noise-margin-down : 80 

I want to write a function with the $line parameter

function extarctText($line)
{
...
return $block;
}

that selects a block that starts with the $line, for example calling $this->extarctText(1/1/6/32) should return the this result ($block) :

line : 1/1/6/32              link-timestamp-down : 2014-06-16:00:06:11
attained-bitrate-down : 3616                       max-bitrate-up : 512                     max-bitrate-down : 2048
threshold-bitrate-up : 256                threshold-bitrate-down : 512                         max-delay-up : 15
max-delay-down : 15                    tgt-noise-margin-up : 60                  tgt-noise-margin-down : 60 
  • Maybe you can use the approach used here http://stackoverflow.com/questions/13246597/how-to-read-a-file-line-by-line-in-php – RST Aug 10 '14 at 08:41

1 Answers1

0

The following snippet will do what you get you the block you need

<?php

function extract_block($filename, $date)
{
    $handle = fopen($filename, "r");
    $block = '';

    $block_start_text = 'line : ' . $date;
    $block_end_text   = 'tgt-noise-margin-down : ';

    $is_inside_block = false;

    while (($line = fgets($handle)) !== false) {

        if($is_inside_block) {
            $block .= $line;

            if(strpos($line, $block_end_text) !== false){
                $is_inside_block = false;
            }
        } else if(strpos($line, $block_start_text) !== false){
            $block .= $line;
            $is_inside_block = true;
        }
    }

    fclose($handle);

    return $block;
}

echo extract_block('input.txt', '1/1/6/33');
bhargavg
  • 1,383
  • 9
  • 12