1.去除空行
<?php
$str = file_get_contents('a.txt');
$str = explode(PHP_EOL, $str); //分割为数组,每行为一个数组元素
$str = array_filter($str); //去除数组中的空元素
$str = implode(PHP_EOL,$str); //用换行符连结数组为字符串
file_put_contents('b.txt',$str);
2.去除每行多余字符
方式一,数组处理
<?php
$str = file_get_contents('a.txt');
$arr = explode(PHP_EOL,$str);
$result = array();
foreach($arr as $v)
{
$result[] = trim(substr($v,6));
}
$result = array_filter($result);
$text = '';
foreach($result as $v)
{
$text .= $v.PHP_EOL;
}
file_put_contents('b.txt',$text);
?>
方式二,正则直接替换
<?php
$str = file_get_contents('a.txt');
$result = preg_replace('/\d+\.\s+/','',$str);
file_put_contents('c.txt',$result);
?>