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;
PHP文件读取与处理
本文介绍了一种使用PHP读取文件夹内多个TXT文件的方法,并通过正则表达式去除注释行,同时利用trim函数移除每行的空白字符。通过对文件内容进行分割和过滤,实现了对文本的有效处理。
285

被折叠的 条评论
为什么被折叠?



