php 去除txt中 连续重复的的章节标题

<?php
set_time_limit(0);
$url="./a.txt";
$file = fopen($url, "r");
$path="cb.txt";
$content=array();
$last_title='';
$m=2;
while(true) {

        if(feof($file)){
        	$now = fgets($file);
            file_put_contents($path, $now.PHP_EOL, FILE_APPEND);
            break;
           
        }else{
  			  $now = fgets($file);
			  if(preg_match_all('/(第[0-9零一二两三四五六七八九十百千万]*(章|章节))(.*)/is', $now, $matches)){
			  	//	echo 'last_title----'.$last_title;
			  		//echo 'now----'.$now;
			  		//echo "<br />";
			  		//print_r($matches);exit;
			  		$biaoti=preg_replace("/( | |	)/","",strip_tags($matches[3][0]));  
					$title=$matches[1][0].' '.$biaoti;
					//echo $now;
					//说明是第一次
					// if($last_title==""){

					// 	$last_title=$now;

					// 	file_put_contents($path, $now,FILE_APPEND);
					// 	//$content[]=$now;
					// }else{

						if($last_title==$title){
							// echo $last_title.'--'.$title;exit;
						 //    echo 'sb';exit;
						    $m=1;
							continue;
						}else{
							$m=0;
							file_put_contents($path, $title,FILE_APPEND);
							$last_title=$title;
						}
					//}
					

			  }else{
			  	  //过滤只有换行符的空行后输出结果只为0;
			  	  //$content=preg_replace("/\s/","",$now); 
				  //echo strlen($content);exit;
				  if($m==1){
				  	$m=0;
				  	continue;
				  }else{
				 	file_put_contents($path, $now,FILE_APPEND);
				  }

			  }
			
        	
        	



        }

}
exit('完成');





?>

去除前

php 去除txt中 连续重复的的章节标题

去除后

 

php 去除txt中 连续重复的的章节标题