Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.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,我正在为我的站点创建一个文本站点地图,看起来像这样: sitemap.txt: https://example.com/ https://example.com/estate/showAll https://example.com/estate/search https://example.com/site/about https://example.com/site/contact https://example.com/post/show/41 https://example.com/pos

我正在为我的站点创建一个文本站点地图,看起来像这样:

sitemap.txt:

https://example.com/
https://example.com/estate/showAll
https://example.com/estate/search
https://example.com/site/about
https://example.com/site/contact
https://example.com/post/show/41
https://example.com/post/show/42
https://example.com/post/show/43
当用户删除帖子42时,我的网站地图应更改为:

https://example.com/
https://example.com/estate/showAll
https://example.com/estate/search
https://example.com/site/about
https://example.com/site/contact
https://example.com/post/show/41
https://example.com/post/show/43
我尝试了这些方法,但没有达到预期效果:

$sitmap = file_get_contents('sitemap.txt');
$line   = 'https://example.com/post/show/42';
$sitmap =str_replace($line, '', $sitmap);
file_put_contents('sitemap.txt', $sitmap);
此代码在sitemap.txt中显示一个空行

$sitmap = file_get_contents('sitemap.txt');
$line   = 'https://example.com/post/show/42';
$sitmap =str_replace($line, '', $sitmap);
$sitmap =str_replace($line, chr(8), $sitmap);
file_put_contents('sitemap.txt', $sitmap);
chr(8)显示退格字符
BS
它不会删除空换行符

我已经用下面的代码完成了,但我认为如果sitemap.txt变大,它会占用太多内存

$sitmap = file_get_contents('sitemap.txt');
$line   = 'https://example.com/posst/show/'. $id;
$sitmap = explode(PHP_EOL, $sitmap);

if ($key = array_search($line, $sitmap)) {
     unset($sitmap[$key]);
}
$sitmap = implode(PHP_EOL, $sitmap);

file_put_contents('sitemap.txt', $sitmap);

有更好的方法吗?

您可以只替换换行符+换行符(
\n
):

正如(见下面的评论)所指出的,
“$line\n”
应该被
$line.PHP\u EOL
替换


这取决于您构建文件的方式。

您只需替换换行符+换行符(
\n
):

正如(见下面的评论)所指出的,
“$line\n”
应该被
$line.PHP\u EOL
替换


这取决于您如何构建文件。

这两种方法都将文件加载到内存中。更好的方法可能是在检查url是否匹配放弃的同时逐行读取,然后同时写入一个新文件,这样您就不会读取整个文件。chr(8)不是LF,即chr(10)。但是为什么要把它们都保存在一个文本文件中呢?在运行时从数据库生成它。然后也许你可以把它缓存到一个文本文件中。你必须知道帖子属于用户42,所以一些浏览网站的机制必须已经到位。更好的方法可能是在检查url是否匹配放弃的同时逐行读取,然后同时写入一个新文件,这样您就不会读取整个文件。chr(8)不是LF,即chr(10)。但是为什么要把它们都保存在一个文本文件中呢?在运行时从数据库生成它。然后也许你可以把它缓存到一个文本文件中。你必须知道这篇文章属于用户42,因此一些浏览网站的机制必须已经到位。可能应该使用PHP_EOL,因为这是独立于平台的,如果没有后续的新行,这可能不适用于最后一个条目。@NigelRen你是对的。我将把这个添加到anwser中。谢谢谢谢@Syscall。我认为这是一个简单而好的方法。我刚将PHP_EOL移到$line的开头,因此,最后一行不会有问题。可能应该使用PHP_EOL,因为这是独立于平台的,如果没有后续新行,这也可能不适用于最后一个条目。@NigelRen你是对的。我将把这个添加到anwser中。谢谢谢谢@Syscall。我认为这是一个简单而好的方法。我刚刚将PHP_EOL移到$line的开头,因此,最后一行不会有问题。
$line = 'https://example.com/post/show/'. $id;
$file = 'sitemap.txt';
file_put_contents($file, 
    str_replace("$line\n", '', file_get_contents($file))
);