如何删除和PHP从文本文件的每行中更新特定行与删除和更新按钮?

问题描述 投票:0回答:1

这是我在文本文件中搜索特定的行代码。现在我想删除和更新结果的比赛之后出现的特定行。但我不是现在该怎么做这个事情。请帮我解决这个问题!

<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';
}

}

php
1个回答
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';
© www.soinside.com 2019 - 2024. All rights reserved.