我想将逐行读取文件返回的字符串传递到函数中。但它给出了一个不寻常的错误。似乎返回的字符串并不完全是 .txt 文件(源文件)中的行。但是,如果我通过复制粘贴手动将字符串传递到函数中,它就可以工作。这里是我的代码:-
<?php
function check($string) { // for removing certain text from the file
$x = 0;
$delete = array();
$delete[0] = "*";
$delete[1] = "/";
for($i=0;$i<2;$i++){
$count=substr_count($string,$delete[$i]);
if($count>0){
$x++;
return false;
break;
}
}
if($x==0)return true;
}
$file = fopen("classlist.txt", "r") or die("Unable to open file!");
$myFile = "new.txt";
$fh = fopen($myFile, "w") or die("can't open file");
while(!feof($file)){
if(check(fgets($file))){
$stringData = fgets($file);
fwrite($fh, $stringData);
}
}
fclose($fh);
?>
我在 new.txt 文件中得到的是:第 2 行第 4 行第 6 行第 8 行------------ 第 21 行 请帮帮我......
最佳答案
每次调用 fgets()
都会从文件中检索新行。每次循环迭代调用一次,将返回的行放入变量中,然后检查并使用该变量。
关于php - 逐行读取文件返回的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7604686/