1

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

sdasd
dasdasda
sadasd

现在我用file_get_contents. 让用户决定他要删除哪个变量。假设他选择了sdasd. 当我现在想sdasd从文本文件中删除行时,我必须使用什么?在这里没有找到任何有用的东西:PHP Manual filesystem

以下是我如何加载 txtfile 的内容:

$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>';
4

5 回答 5

0

哦,男孩,你的方法有很多错误,我什至不知道从哪里开始。

不管怎样,我们会努力的。首先让我们看清楚:

<?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);
}
于 2012-11-15T10:46:02.620 回答
0

您必须将文件读入内存以促进用户交互。当用户选择要删除的变量时,将文件写回而不删除要删除的项目。

于 2012-11-15T10:13:33.220 回答
0

这是我的做法:

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. 
于 2012-11-15T10:16:47.317 回答
0

要将所有行放入数组中:

$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);
于 2012-11-15T10:20:43.810 回答
0

你可以使用这样的东西。

$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);

如果要删除多行,请更改

$wordlist = "sdasd";

$wordlist = "sdasd|abcd"; //this will remove abcd also if found.
于 2012-11-15T10:32:47.700 回答