将文件中的空格转换成\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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章