Php 删除文本文件中的空行
我有一个文件,在两个空行的开头和结尾都有一个空的Righa,这会给我带来一个错误Php 删除文本文件中的空行,php,Php,我有一个文件,在两个空行的开头和结尾都有一个空的Righa,这会给我带来一个错误 <?php $file = "https://example.com/text/file.txt"; $fr = fopen($file, 'r'); $contenuto = ""; while (!feof($fr)) { $riga = fgets($fr); $inizio = strpos($riga, "Capitolo"); $nuova_riga = subs
<?php
$file = "https://example.com/text/file.txt";
$fr = fopen($file, 'r');
$contenuto = "";
while (!feof($fr)) {
$riga = fgets($fr);
$inizio = strpos($riga, "Capitolo");
$nuova_riga = substr($riga, $inizio);
$contenuto .= $nuova_riga . "";
}
fclose($fr);
$fr = fopen("file.txt", 'w');
fwrite($fr, $contenuto);
fclose($fr);
注意:未定义的偏移量:
注意:未定义索引:
如果手动删除空行,显然我没有错误,因此我应该打开文件以写入抑制空行,然后继续页面上的其他说明。我该怎么办?谢谢你没有完全理解你的问题。也许是吧
str_replace("\n\n", "\n", $file)
没有完全理解你的问题。也许是吧
str_replace("\n\n", "\n", $file)
你可以这样使用它
<?php
ini_set('display_errors', 1);
$resultant=array();
$lines=file("/path/to/your/test.txt");//retrieving lines of the file.
foreach($lines as $value)
{
if($value!="\n")
{
$resultant[]=$value;
}
}
$string=implode("", $resultant);//combining array into string with null
file_put_contents("/path/wher/you/want/to/save.txt", $string);
你可以这样使用它
<?php
ini_set('display_errors', 1);
$resultant=array();
$lines=file("/path/to/your/test.txt");//retrieving lines of the file.
foreach($lines as $value)
{
if($value!="\n")
{
$resultant[]=$value;
}
}
$string=implode("", $resultant);//combining array into string with null
file_put_contents("/path/wher/you/want/to/save.txt", $string);
请尝试使用支持标志的
file_put_contents('file.txt',implode('', file('https://example.com/text/file.txt', FILE_SKIP_EMPTY_LINES)));
请尝试使用支持标志的
file_put_contents('file.txt',implode('', file('https://example.com/text/file.txt', FILE_SKIP_EMPTY_LINES)));
你的代码兄弟在哪里?你的代码兄弟在哪里?用这种方法不要删除空行,而是在行和行之间添加空行other@Michael我已经更新了我的帖子,你可以检查一下,它可以正常工作。我已经测试过了。它不起作用,所以在开始和结束时的空白行仍然用记事本+ +查看行包含CR和LF是可能消除的。these@Michael你能分享你的文件样本吗?文件是按POST报告的方式创建的,不删除空行,但在行和行之间添加空行other@Michael我已经更新了我的帖子,你可以检查一下,它可以正常工作。我已经测试过了。它不起作用,所以在开始和结束时的空白行仍然用记事本+ +查看行包含CR和LF是可能消除的。these@Michael你能分享你的文件样本吗?该文件是根据post上的报告创建的