如何在PHP文本文件中删除数组

时间:2015-11-26 03:34:12

标签: php arrays search

我有一个文本文件。我想删除一些带有搜索查询的行。 阵列是逐行的。我想把它变成http://keywordshitter.com/

逻辑是,

搜索 - >在阵列中 - >输出是阵列没有"查询和#34;

我尝试过的代码:

$fileset = file_get_contents("file.txt");
$line = explode("\n", $fileset);
$content = array_search("query",$line);
print_r($content);

我的file.txt

one
two
three
apple
map

我使用array_search但没有工作。

2 个答案:

答案 0 :(得分:0)

在爆炸功能上使用PHP_EOL而不是“\ n”。 PHP_EOL将处理服务器平台的正确换行符。

答案 1 :(得分:0)

你可以像

那样进行搜索
$fileset=file("file.txt");   // file function reads entire file into an array
$len=count($fileset);
for($i=0;$i<$len;$i++)
  {
       if($fileset[$i]=="query")
          {
               $fileset[$i]="";
               break;                 //then we will stop searching
          }
   }
$fileset_improve=implode($fileset);    //this will again implode your file
$handle=fopen("file.txt","w");        //opening your file in write mode
fwrite($handle,$fileset_improve);     //writing file with improved lines
fclose($handle);                    //closing the opened file

请记住,这些行会使您的搜索行blank ....

如果你愿意那么你可以安排整个数组,即shifting following indexed data to previous index来减少行数,但这会增加你的编程复杂性。

希望这对你有用。

由于