我正在尝试删除基于ipaddress的大型文本文件中的某些行集。 60,000行。每个行集都从MaxBytes [ipaddress]开始,以</TABLE>
结尾,并且在每个行集之间存在一个空行。文本文件中的表格行有所不同。
示例行集:
MaxBytes[192.168.1.1]: 10000 <--start line
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>300</TD></TR>
</TABLE> <-- end line (Need to delete lines from start to end line)
我正在尝试使用以下代码(由Yerke支持)查找起始行,但无法找到查找包含</table>
标签的下一行编号的方法。我需要找出包含特定ipaddress的行集的开始和结束行号并将其删除。
我是编码的初学者,所以我可能需要更多的指导。
代码:
<?php
$dir = "example.txt";
$searchstrt = "192.168.1.1";
///// find details
function find_line_number_by_string($dir, $searchstrt, $case_sensitive=false ) {
$line_number = [];
if ($file_handler = fopen($dir, "r")) {
$i = 0;
while ($line = fgets($file_handler)) {
$i++;
//case sensitive is false by default
if($case_sensitive == false) {
$searchstrt = strtolower($searchstrt);
$line = strtolower($line);
}
//find the string and store it in an array
if(strpos($line, $searchstrt) !== false){
$line_number[] = $i;
}
}
fclose($file_handler);
}else{
return "File not exists, Please check the file path or dir";
}
return $line_number;
}
$line_number = find_line_number_by_string($dir, $searchstrt);
var_dump($line_number);
?>
示例example.txt
MaxBytes[192.168.1.1]: 10000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>300</TD></TR>
</TABLE>
MaxBytes[192.168.1.2]: 30000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>300</TD></TR>
<TR><TD>Name:</TD> <TD>ABC</TD></TR>
</TABLE>
MaxBytes[192.168.1.3]: 10000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>200</TD></TR>
<TR><TD>Location:</TD> <TD>INDIA</TD></TR>
</TABLE>
我找到了一些解决方法,以获取包含所需IP地址的行集的行号。有没有人建议更好的方法。
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$dir = "example.txt";
$searchstrt = "192.168.1.2";
$searchend = "</TABLE>";
///// find details
function find_line_number_by_string($dir, $searchstrt, $case_sensitive=false ) {
$line_number = [];
if ($file_handler = fopen($dir, "r")) {
$i = 0;
while ($line = fgets($file_handler)) {
$i++;
//case sensitive is false by default
if($case_sensitive == false) {
$searchstrt = strtolower($searchstrt);
$line = strtolower($line);
}
//find the string and store it in an array
if(strpos($line, $searchstrt) !== false){
$line_number[] = $i;
}
}
fclose($file_handler);
}else{
return "File not exists, Please check the file path or dir";
}
return $line_number;
}
$line_number = find_line_number_by_string($dir, $searchstrt);
//var_dump($line_number);
$start = $line_number[0];
////////////////////////
function find_line_number_by_string1($dir, $searchend, $case_sensitive=false, $start) {
$line_number1 = [];
if ($file_handler1 = fopen($dir, "r")) {
$i = $start;
// $i = 0;
while ($line1 = fgets($file_handler1)) {
$i++;
//case sensitive is false by default
if($case_sensitive == false) {
$searchend = strtolower($searchend);
$line1 = strtolower($line1);
}
//find the string and store it in an array
if(strpos($line1, $searchend) !== false){
$line_number1[] = $i;
}
}
fclose($file_handler1);
}else{
return "File not exists, Please check the file path or dir";
}
return $line_number1;
}
$line_number1 = find_line_number_by_string1($dir, $searchend, $case_sensitive=false, $start);
$first = $line_number[0];
$last = $line_number1[0];
//var_dump($line_number1);
for ($x = $first; $x <= $last; $x++) {
echo "Line number to be delete : $x <br>";
}
?>
答案 0 :(得分:0)
我找到了解决问题的方法。我刚刚在现有代码中添加了几行。现在,它可以按要求正常工作。
$lines = file($dir, FILE_IGNORE_NEW_LINES);
for ($x = $first; $x <= $last; $x++) {
echo "Line number to be delete : $x <br>";
$lines[$x] = '';
unset($lines[$x]);
}
//var_dump($lines);
file_put_contents($dir , implode("\n", $lines));