我的文字文件是:
;=-= title1-id1 =-=;
zzzzzzzzzzzz
xzqwnqrj90x9
n9ndfpo-f,m129
mf20fmf2-m,
;=-= end-title1-id1 =-=;
;=-= title2-id2 =-=;
zzzzzzzzzzzz
xzqwnqrj90x9
n9ndfpo-f,m129
mf20fmf2-m,
;=-= end-title2-id2 =-=;
我需要阅读此文件并删除我想要的内容,例如:
从;=-= title2-id2 =-=;
到;=-= end-title2-id2 =-=;
答案 0 :(得分:1)
你的目标是什么?并问自己,我有任何其他解决方案来实现它。 顺便说一句,如果你问我&你没有很多选择。 在此文件中,您可以搜索文件内容的一部分,然后在提交后,您的搜索结果将被删除。
<form action="" method="post">
<input type="text" name="search">
<input type="submit" name="searching">
<p>
for example= insert "zzzzzzzzzzzz"
</p>
</form>
<?php
if(isset($_POST['searching']))
{
$file = 'test.txt';
$searchfor = $_POST['search'];
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches))
{
$action = str_replace($searchfor, '', $contents);
file_put_contents($file, $action);
}
}
答案 1 :(得分:1)
file_get_contents获取整个文件,以便填充内存 我很快就遇到了这样的事情。
$myArray = array();
$file = "file.txt";
$bytesStreamed = 1024; //max line length times 8
$tempString = "";
$iterator = 1;
$readCode = true;
if($stream = fopen($file, 'r')) {
//stream_get_contents gets only few bytes of the file so does not use up all the memory for big files
while($streamKiloByte = stream_get_contents($stream, $bytesStreamed)) {
$temp = explode("\n", $tempString.$streamKiloByte);
$tempString = "";
for($i = 0; $i<(count($temp)-1); $i++){
if($i==0) {
//last element can be partial, lets keep it
$tempString = $temp[count($temp)-1];
} else {
array_push($myArray, $temp[$i-1]);
}
}
while(count($myArray)){
foreach($myArray AS $whatEver){
$firstElementOfArray = array_shift($myArray);
$f =& $firstElementOfArray;
if($f!="\r"){
if($f ==";=-= title{$iterator}-id{$iterator} =-=;\r"){
//the useless code started
$readCode = false;
} else if ($f == ";=-= end-title{$iterator}-id{$iterator} =-=;\r" || $f == ";=-= end-title{$iterator}-id{$iterator} =-=;"){
//the useless code ended, preapered for next block
$readCode = true;
$iterator++;
} else {
if($readCode){
//THE CODE
echo $f;
} else {
//the useless code
}
}
}
}
}
}
fclose($stream);
}
在文件中使用它:
;=-= title1-id1 =-=;
zzzzzzzzzzzz
xzqwnqrj90x9
n9ndfpo-f,m129
mf20fmf2-m,
;=-= end-title1-id1 =-=;
THE CODE 1
;=-= title2-id2 =-=;
zzzzzzzzzzzz
xzqwnqrj90x9
n9ndfpo-f,m129
mf20fmf2-m,
;=-= end-title2-id2 =-=;
THE CODE 2
;=-= title3-id3 =-=;
zzzzzzzzzzzz
xzqwnqrj90x9
n9ndfpo-f,m129
mf20fmf2-m,
;=-= end-title3-id3 =-=;
THE CODE 3
;=-= title4-id4 =-=;
zzzzzzzzzzzz
xzqwnqrj90x9
n9ndfpo-f,m129
mf20fmf2-m,
;=-= end-title4-id4 =-=;
得到了回复:
THE CODE 1THE CODE 2THE CODE 3
P.S。将代码流式传输到一个文件,以便它可以记忆。
答案 2 :(得分:1)
我制作了自己的剧本,但我担心它需要优化......但是它运作正常:
<?php
$filename = 'file.txt';
$fisttag = ';=-= title2-id2 =-=;';
$lasttag = ';=-= end-title2-id2 =-=;';
// Open the file
$fp = @fopen($filename, 'r');
// Add each line to an array
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}
$i=0;
$f = 0;
$newcont = "";
$blp = 0;
while(isset($array[$i]))
{
//if(($array[$i]=="\n")and($array[$i+1]=="\n")){$blp=1;die($blp);}
if($array[$i]==$fisttag){ $f=1; }
if($f==1){ if($array[$i]==$lasttag){ $f=0; } }
if(($f!=1)and($array[$i]!=$lasttag))
{
if($i>0 and ($array[$i-1]==$lasttag)){ $blp=1; }
if($blp!=1){$newcont .= $array[$i]."<br />";}
else $blp = 0;
}
$i++;
}
echo $newcont;
?>