我有多个包含相同文本结构的文件。我现在正在尝试删除所有行,直到一行以特定单词开头。 这是文件之一的一部分:
Test Sampertant
ALL 5784
COMMENT This files contains information about infomarxinc
COMMENT Companie located in USA
FEATURES Location/Qualifiers
A lines (7709..2170)
3'try complement(7676..7678)
/note="stop"
/label=STOP
B lines (7679..7708)
/note="stop"
/label=start
PAST
1 talian and American multinational corporation and is the world’s
50 eighth largest auto maker.The group was established in late 2014
我只想保留PAST之后的行 我已经编写了以下代码来做到这一点
$lines = file($newname);
# Loop through the array
foreach($lines as $line) {
$seq = trim($line);
# Find all lines starting with a number
if (preg_match('/^\d/', $seq)){
# Replace all number with |
$seq = preg_replace('/[0-9]+/', '', $seq);
$seq = preg_replace('/\s/',"",$seq);
# Store in string
$out .= $seq;
}
### Read lines into file ###
$f = fopen($newname, "w");
fwrite($f, $out);
fclose($f);
}
对于大多数文件,直到我得到此文件,它都可以工作。 PART之前的一行以3'try开始。在最终结果中,还添加了3'try,但我不希望这样做。 现在如何删除所有行,直到我的行以PAST行开头,然后执行我的代码以查找以数字开头的所有行。 要仅保留此文件的这些行:
1 talian and American multinational corporation and is the world’s
50 eighth largest auto maker.The group was established in late 2014
答案 0 :(得分:2)
您可以添加一些逻辑来首先找到“ PART”行,然后再写出编号的行:
...
$lines = file($newname);
$found = false;
// Loop through the array
foreach($lines as $line) {
$seq = trim($line);
if( $seq == "PAST" )
$found = true;
// Find all lines starting with a number
if ($found && preg_match('/^\d/', $seq)){
# Replace all number with |
$seq = preg_replace('/[0-9]+/', '', $seq);
$seq = preg_replace('/\s/',"",$seq);
# Store in string
$out .= $seq;
}
// Read lines into file
$f = fopen($newname, "w");
fwrite($f, $out);
fclose($f);
}
答案 1 :(得分:1)
也许我遗漏了一些东西,但是以下应该起作用:
$raw = file_get_contents($filename);
if (! $raw) {
echo 'no valid data';
exit;
}
$cut = strpos($raw,'PAST');
if (! $cut) {
echo 'PAST not found in file';
exit;
}
echo substr($raw,$cut + 5);
exit;
您所说的所有文件都具有相同的结构:
$raw = file_get_contents($filename);
if (! $raw) {
echo 'no valid data';
exit;
}
$lines = explode("\n",$raw); // assume \n as the line return
$lines = array_splice($lines,13);
echo join("\n",$lines);
exit;