將文件中的空格轉換成\t製表符,去掉末尾的空格及\t製表符

<?php header("Content-type: text/html; charset=utf-8");set_time_limit(0);?>
將文件中的空格轉換成\t製表符,去掉末尾的空格及\t製表符
<form name="myform" id="myform" action="" method="post">
<table width="100%" border="0">
<tr>
	<td width="80">轉換路徑:</td>
	<td width="180"><input name="file" type="text" value="<?php echo isset($_POST['file'])?$_POST['file']:''?>"/></td>
	<td>輸入文件路徑或目錄路徑,此處爲相對地址,留空表示當前目錄</td>
</tr>
</tr>
	<td>轉換模式:</td>
	<td><select name="type"><option value="0" <?php echo isset($_POST['type']) && $_POST['type']=='0'?'selected="selected"':''?>>當前目錄</option><option value="1" <?php echo isset($_POST['type']) && $_POST['type']=='1'?'selected="selected"':''?>>包含子目錄</option></select></td>
	<td>當轉換路徑爲目錄時,此處用來設置是否遞歸轉換子目錄裏面的文件格式</td>
</tr>
</tr>
	<td>確認轉換:</td>
	<td><select name="convert"><option value="0" <?php echo isset($_POST['convert']) && $_POST['convert']=='0'?'selected="selected"':''?>>否</option><option value="1" <?php echo isset($_POST['convert']) && $_POST['convert']=='1'?'selected="selected"':''?>>是</option></select></td>
	<td>轉換之前可以先看一下待轉換的文件列表,確認沒有問題後再進行轉換</td>
</tr>
</table>
<td><input type="submit" id="dosubmit" class="button" name="dosubmit" value="提交"/></td>
</form>
<?php
if($_POST){
	$path=dirname(__FILE__);
	$file=$_POST['file']?$path.DIRECTORY_SEPARATOR.$_POST['file']:$path;
	$type=$_POST['type'];
	$convert=$_POST['convert'];
	if(is_file($file) || is_dir($file)){
		myconvert($file, $type, $convert);
	}else{
		echo '提示:你輸入的文件或目錄不存在';
	}
}
function myconvert($dir, $type, $convert, $tag=1){
	if(is_file($dir)){
		$self=substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/')+1);
		if(strpos($dir, $self)===false){
			if($convert){
				$str=file($dir);
				foreach($str as $key=>$val){
					$val=rtrim($val);
					$val=str_replace("\t", '    ', $val);
					$val=str_replace('    ', "\t", $val);				
					$str[$key]=$val;
				}
				file_put_contents($dir, implode("\r\n", $str));
				echo "文件: $dir 轉換成功<br>";
			}else{		
				echo "文件: $dir<br>";
			}
		}
	}else{
		if($type || $tag){
			$dirs = glob($dir.DIRECTORY_SEPARATOR.'*');
			foreach($dirs as $file){
				myconvert($file, $type, $convert, 0);
			}
		}
	}
}

發佈了30 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章