我有一些这样的文字:
(此文件包含一些文本文件块,以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
答案 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');