Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 替换文件中的行不会删除空行_Php - Fatal编程技术网

Php 替换文件中的行不会删除空行

Php 替换文件中的行不会删除空行,php,Php,我有一个从txt文件中删除行的函数: function replace_file($path, $string, $replace) { set_time_limit(0); if (is_file($path) === true) { $file = fopen($path, 'r'); $temp = tempnam('./', 'tmp'); if (is_r

我有一个从txt文件中删除行的函数:

function replace_file($path, $string, $replace)
    {
        set_time_limit(0);

        if (is_file($path) === true)
        {
            $file = fopen($path, 'r');
            $temp = tempnam('./', 'tmp');

            if (is_resource($file) === true)
            {
                while (feof($file) === false)
                {
                    file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND);
                }

                fclose($file);
            }

            unlink($path);
        }

        return rename($temp, $path);
    }
用法:
replace_文件(“file.txt”,“要删除的行”,“要删除的行”)

它工作得很好,但它总是在文件末尾保留一个空行

有可能解决这个问题吗

任何帮助都将不胜感激


谢谢

如果只想删除结尾的空行,请查看。

如果只想删除结尾的空行,请查看。

我将函数编辑为:

function replace_file($path, $string, $replace)
    {
        set_time_limit(0);

        if (is_file($path) === true)
        {
            $file = fopen($path, 'r');
            $temp = tempnam('./', 'tmp');

            if (is_resource($file) === true)
            {
                while (feof($file) === false)
                {
                    file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND);
                    file_put_contents($temp,
                        implode('', file($path, FILE_SKIP_EMPTY_LINES)));
                }

                fclose($file);
            }

            unlink($path);
        }

        return rename($temp, $path);
    }

现在它可以工作了。

我对函数进行了如下编辑:

function replace_file($path, $string, $replace)
    {
        set_time_limit(0);

        if (is_file($path) === true)
        {
            $file = fopen($path, 'r');
            $temp = tempnam('./', 'tmp');

            if (is_resource($file) === true)
            {
                while (feof($file) === false)
                {
                    file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND);
                    file_put_contents($temp,
                        implode('', file($path, FILE_SKIP_EMPTY_LINES)));
                }

                fclose($file);
            }

            unlink($path);
        }

        return rename($temp, $path);
    }

现在它可以工作了。

通常认为在文件末尾留下空行是正确的。如果您只处理小文件,那么使用和数组walk-then内爆到一个文件中也会起到同样的作用,但是对于大小文件来说,一个更为理想的解决方案是,当使用文件时,您无法用当前代码解决问题,并且不知道这是否是最后一行来修剪新行。@为什么?(从技术上讲)。当我遇到它时,我总是把它拿走;不知道我是否应该停下来,lol.@J.ScottElblein这是旧系统的遗留问题,旧系统希望在EOF之前出现空行。在PHP中,它是PSR-2的一部分。在函数调用中,将其设置为零是一个非常糟糕的主意。函数不应该有这样的副作用。通常认为在文件末尾留下空行是正确的。如果您只处理小文件,那么使用和数组walk-then内爆到一个文件中也会起到同样的作用,但是对于大小文件来说,一个更为理想的解决方案是,当使用文件时,您无法用当前代码解决问题,并且不知道这是否是最后一行来修剪新行。@为什么?(从技术上讲)。当我遇到它时,我总是把它拿走;不知道我是否应该停下来,lol.@J.ScottElblein这是旧系统的遗留问题,旧系统希望在EOF之前出现空行。在PHP中,它是PSR-2的一部分。在函数调用中,将其设置为零是一个非常糟糕的主意。函数不应该有那样的副作用。