php讀取txt文件並分割行替換空字符串
$handler = opendir('zhuzhuoquan');//文件夾名
$ii=0;
$str = '';
while( ($filename = readdir($handler)) !== false )
{
if($filename =='.' || $filename=='..' || $filename=='views') continue;
$ii++;
//echo $ii.'. '.$filename.'<br>';
$file_path = 'zhuzhuoquan/'.$filename;
$str .=trim_file($file_path);
}
echo $str;
function trim_file($file_path)
{
$file = fopen($file_path, "r");
$user=array();
$i=0;
$str = '';
//輸出文本中所有的行,直到文件結束爲止。
while(! feof($file))
{
$row = fgets($file);
$row = trim($row);
if(!$row) continue;
$row_first = substr($row, 0, 2);// /* // *
$row_first1 = substr($row, 0, 7);// defined
if(in_array($row_first, array("/*", "//", "* ", "*/","<?"))) continue;
if(in_array($row_first1, array("defined"))) continue;
$row_first2 = substr($row, 0, 5);// defined
if(in_array($row_first2, array("class")))
{
$row = "/* End of class */ \r\n".$row;
}
$user[$i]= $row;//fgets()函數從文件指針中讀取一行
$str .= $row."\r\n";
$i++;
}
fclose($file);
$user=array_filter($user);
//print_r($user);
return $str;
}
exit;
$a = "ss.txt";
$file = fopen($a, "r");
$ary=array();
$i=0;
$str = '';
//輸出文本中所有的行,直到文件結束爲止。
while(! feof($file))
{
$row = fgets($file);
$row = trim($row);
if(!$row) continue;
$ary[$i]= $row;//fgets()函數從文件指針中讀取一行
$str .= $row."\r\n";
$i++;
}
fclose($file);
$ary=array_filter($ary);
echo $str;exit;
exit;