Version: 1.0

Type: Full Script

Category: Other

License: Other

Description: If you, like me, are too lazy to indent your php or javascript code, this little php will do it for you. It'll intent using tabs, searching for curly brackets - '{' and '}'. Makes your code nice and tidy. Just place it in the same folder as the files you want to indent. Enjoy.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>INDENT</title>
</head>
<body>

<button onclick="location='indent.php?do=yes'">INDENT FILES</button>
<br>

<?

if($do)
{
	indentdir('');
	// Here, add your own subdirs like so:
	// indentdir('subdir');
}

function indentdir($dn)
{
	$formats=array('js','php');
	$d=dir($dn);
	while($fn=$d->read())
	{
		$format=end(explode('.',$fn));
		if(in_array($format,$formats)){
			if($dn){
			$ffn=$dn.'/'.$fn;}
			else{
			$ffn=$fn;}
			if(is_file($ffn))
			{
				$r=indent($ffn);
				if($r){
					print $ffn."<br>\n";
				}
			}
		}
	}
	$d->close();
}

function indent($fn)
{
	$text = file($fn);
	
	$a=implode('',$text);
	if(!ereg('{',$a)){
		// }
	return false;}
	
	$change=false;
	
	$asc1=ord('{');
	$asc2=ord('}');
	
	$lnm=count($text);
	
	$level=0;
	for($ln=0;$ln<$lnm;$ln++)
	{
		
		$line=$text[$ln];
		
		$line=trim($line);
		
		$chars=count_chars($line,1);
		
		$level-=$chars[$asc2];
		
		if($level>=0)
		{
			$tabs=str_repeat("\t",$level);
			
			$change=($change or ($text[$ln]!=$tabs.$line."\n"));
			
			$text[$ln]=$tabs.$line."\n";
		}
		
		
		$level+=$chars[$asc1];
		
	}
	
	if($change)
	{
		$text=implode('',$text);
		
		$f=fopen($fn,'w');
		fwrite($f,$text,strlen($text));
		fclose($f);
	}
	
	return $change;
}
?>

</body>
</html>