如何使用php中文本文件的每一行中的删除和更新按钮删除和更新特定行?

时间:2019-02-05 19:02:39

标签: php

这是我的代码,用于搜索文本文件中的特定行。现在,我要删除并更新在结果匹配后出现的特定行。但是我现在不怎么做。请帮我解决这个问题!

<form action="dis.php" method="post">
    <label>Name:</label>
    <input type="text" name="search">
    <input type="submit" name="send" value="Search">
</form>


<?php

if (isset($_POST['send'])) {
    $search_txt = $_POST['search'];

    $search = $search_txt;
$lines = file('file.txt');
// Store true when the text is found
$found = false;
foreach($lines as $line)
{
  if(strpos($line, $search) !== false)
  {
    $found = true;
    echo '<h1>The Folowing Contacts Exists against your search:</h1><br> '.$line."<br>";
  }
}
// If the text was not found, show a message
if(!$found)
{
  echo 'No match found';
}

}

1 个答案:

答案 0 :(得分:0)

您可以简单地输入一个变量来计算文件中的行数,然后基于该变量进行插入吗?

$found = false;
$currentRow=0;
foreach($lines as $line)
{
    $currentRow++;  
if(strpos($line, $search) !== false)
  {
    $found = true;
    echo '<h1>The Folowing Contacts Exists against your search:</h1><br> '.$line." 
<br>";
  }
  }

$lines[$currentRow] = 'my modified line';