php基于变量删除文本文件中特定的txtline

php基于变量删除文本文件中特定的txtline,php,Php,嘿,伙计们,有没有办法删除文本文件中的特定变量?我有一个文本文件,我们称之为xyz.txt,其中包含以下内容: sdasd dasdasda sadasd 现在我正在加载带有file\u get\u contents的文件内容。让用户决定要删除哪个变量。假设他选择了sdasd。当我现在想从文本文件中删除带有sdasd的行时,我必须使用什么?在这里没有发现任何有用的东西: 下面是我如何加载TXT文件的内容: $handle = @fopen("xyz.txt", "r"); ec

嘿,伙计们,有没有办法删除文本文件中的特定变量?我有一个文本文件,我们称之为xyz.txt,其中包含以下内容:

sdasd
dasdasda
sadasd
现在我正在加载带有
file\u get\u contents
的文件内容。让用户决定要删除哪个变量。假设他选择了
sdasd
。当我现在想从文本文件中删除带有
sdasd
的行时,我必须使用什么?在这里没有发现任何有用的东西:

下面是我如何加载TXT文件的内容:

$handle = @fopen("xyz.txt", "r");
        echo ('<table class="table table-bordered table-striped">');
      echo '<thead>';
      echo'<tr>';
        echo'<th>Auswahl</th>';
        echo'<th>Admin</th>';
        echo'</tr>';
        echo'</thead>';
        echo ('<tbody>');
        echo("<tr>");
        while (!feof($handle)) // Loop til end of file.
        {
        $buffer = fgets($handle, 4096);
        // Read a line.
        list($a,$b,$c)=explode(" ",$buffer);
        //Separate string by the means of |
        echo '<td><form name="Lager" method="submit" action="admins_verwalten.php"><input type="radio" name="Admin" value="'.$a.$b.$c.'"><br></td>';
        echo('<td>'.$a.$b.$c."</td>");
        echo("</tr>");
        }
        echo ('</tbody>');
        echo ('</table>');
        echo '<button type="submit" class="btn btn-primary"></i>Admin delete</button></label></form>';
$handle=@fopen(“xyz.txt”、“r”);
回声(“”);
回声';
回声';
echo'Auswahl';
回音“Admin”;
回声';
回声';
回声(“”);
回声(“”);
而(!feof($handle))//循环直到文件结束。
{
$buffer=fgets($handle,4096);
//读一行。
列表($a,$b,$c)=分解(“,$buffer);
//通过以下方式分隔字符串:|
回声“
”; 回声('.$a.$b.$c.''); 回声(“”); } 回声(“”); 回声(“”); 回显“管理员删除”;
您必须将文件读入内存以方便用户交互。当用户选择要删除的变量时,将文件写回,但不包含要删除的项。

以下是我的操作方法:

1. Read contents from the file into a variable
2. use the explode with a linefeed as the delimiter to put the lines of the text file into an array.
3. take user input.
4 for each element in the array see if user input matches the value.
5. if matches then delete that element in the array.
6. recreate the text file from the array and write to disk. 

要将所有行放入数组,请执行以下操作:

$arr = file('textfile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
将字符串取出:

$to_remove = 'asdf';
$arr = array_filter($arr, function($item) use ($to_remove) {
    return $item != $to_remove;
});
要回写文件,请执行以下操作:

file_put_contents('textfile.txt', join("\n", $arr));
编辑

帮助您调试筛选不起作用的原因;例如,这是有效的:

$arr = array('HAUGMA1', 'sdasd', 'dasdasda', 'sadasd');

$to_remove = 'sdasd';
$arr = array_filter($arr, function($item) use ($to_remove) {
        return $item != $to_remove;
});

print_r($arr);

你可以用这样的东西

$source = "xyz.txt";
$raw = file_get_contents($source) or die("Cannot read file");
$wordlist = "sdasd";
$raw = preg_replace("/($wordlist)/ie", "", $raw);
file_put_contents($source, $raw);
如果要删除多行, 改变


哦,孩子,你的方法有这么多错误,我甚至不知道从哪里开始

无论如何,我们会努力的。首先,让我们把视野弄清楚:

<?php
    // Read the contents into an array without newlines
    $content = file('xyz.txt', FILE_IGNORE_NEW_LINES);
?>
<!-- A single form is enough, we don't want a gazillion forms!!! -->
<form name="Lager" method="POST" action="admins_verwalten.php">
<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Auswahl</th>
            <th>Admin</th>
        </tr>
    </thead>
    <tbody>
    <?php
        for ( $i=0; $i<$content.length; $i++ ) {
            // We don't need the line itself as the input's value,
            // line number will suffice
            // Moreover we will allow multiple lines to be deleted (that's
            // what the >> name="Admin[]" << is for
            print sprintf(
                '<tr>
                     <td><input type="radio" name="Admin[]" value="%d"></td>
                     <td>%s</td>
                 </tr>',
                $i,
                $content[$i]
            );
        }
    ?>
    </tbody>
</table>
<button type="submit" class="btn btn-primary" value="Admin delete"/>
</form>

文本文件不包含变量,它包含。。。那么文本,;所以你需要字符串操作函数。你不想替换并把它放回去吗?这里的
文件内容在哪里,在代码中?我已经设法将内容加载到一个表中,用户可以通过单选按钮选择每一行。我不知道如何在没有选择的情况下写回所有内容。当用户返回表单时,您必须再次读取整个文件。然后找到要删除的行,并在没有该行的情况下将文件写入磁盘-有效地删除它。因此,没有任何函数可以用简单的方式执行此操作?不幸的是,没有简单的函数可用于此操作。尽管php确实提供了一些帮助程序,允许您快速编辑文件的内容。您选择的任何方法都需要您读取文件,找到要删除的行,然后在没有该行的情况下写入文件。因此,我对其进行了测试,但没有发生任何问题。因此,我创建了一个echo,输出$arr,它只说
Array
@Pgr456您需要
print\r($arr)
如果您希望调试数组内容。这将为我提供
数组([0]=>HAUGMA1[1]=>sdasd[2]=>DASDA[3]=>sadasd)
好,我已经调试了它,似乎它没有过滤array@Pgr456
$to_remove
应分配无线电输入框的值。
$wordlist = "sdasd|abcd"; //this will remove abcd also if found.
<?php
    // Read the contents into an array without newlines
    $content = file('xyz.txt', FILE_IGNORE_NEW_LINES);
?>
<!-- A single form is enough, we don't want a gazillion forms!!! -->
<form name="Lager" method="POST" action="admins_verwalten.php">
<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Auswahl</th>
            <th>Admin</th>
        </tr>
    </thead>
    <tbody>
    <?php
        for ( $i=0; $i<$content.length; $i++ ) {
            // We don't need the line itself as the input's value,
            // line number will suffice
            // Moreover we will allow multiple lines to be deleted (that's
            // what the >> name="Admin[]" << is for
            print sprintf(
                '<tr>
                     <td><input type="radio" name="Admin[]" value="%d"></td>
                     <td>%s</td>
                 </tr>',
                $i,
                $content[$i]
            );
        }
    ?>
    </tbody>
</table>
<button type="submit" class="btn btn-primary" value="Admin delete"/>
</form>
// This will give us an array with numbers of lines to be deleted
$lines = isset($_POST['Admin']) ?
             $_POST['Admin'] : false;

if ( !empty($lines) ) {
    $content = file('xyz.txt');

    foreach ( $lines as $line ) {
        // Delete the lines chosen by the client
        unset($content[$line]);
    }

    // Write back the remaining content
    file_put_contents('xyz.txt', $content);
}