如何在特定单词后选择字符串的一部分?

时间:2014-08-10 08:10:22

标签: php

我有一些这样的文字: (此文件包含一些文本文件块,以line : x/x/x/x开头,以tgt-noise-margin-down : x词组结尾)

------------------------------------------------------------------------------------------------------------------------------------
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 

我想用$line参数

编写一个函数
function extarctText($line)
{
...
return $block;
}

选择以$line开头的块,例如调用$this->extarctText(1/1/6/32)应该返回此结果($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 

1 个答案:

答案 0 :(得分:0)

以下代码段将为您提供所需的块

<?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');