strip_whitespace()
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;