$value) { if(is_array($value)) { stripslashes_array($array[$key], $iterations + 1); } else { $array[$key] = stripslashes($array[$key]); } } } } if(get_magic_quotes_gpc()) { //stripslashes_array($_GET); stripslashes_array($_POST); //stripslashes_array($_COOKIE); } function h($s) { echo htmlspecialchars($s, ENT_QUOTES); } function normalize_indent($code, $num_of_spaces_to_indent) { /* This function is a port/fork of [Michael Stum](http://meta.stackoverflow.com/users/91/michael-stum)'s * #C/.NET code . */ $spaces = str_repeat(' ', $num_of_spaces_to_indent); $removeIndent = 0; $code = str_replace("\t", ' ', $code); $code = $code . " \n\t"; //append an extra line with a some non-space character (tab) or code with no line breaks fail to parse correctly $codeLines = preg_split("/\012\015?/", $code); $codeLineLastIndex = count($codeLines) - 1; foreach($codeLines as $s) { if($s === '') continue; $tmp = 0; foreach(str_split($s) as $c) { if ($c != " ") { break; } $tmp++; } if($removeIndent == 0 || $tmp < $removeIndent) { $removeIndent = $tmp; } } $lineCounter = 0; $codeFormatted = ''; foreach($codeLines as $s) { if($lineCounter == $codeLineLastIndex) break; //don't include appended extra line $codeFormatted .= $spaces; $codeFormatted .= (strlen($s) >= $removeIndent ? substr_replace($s, '', 0, $removeIndent) : $s) . "\n"; $lineCounter++; } return $codeFormatted; } $code = isset($_POST['code']) ? $_POST['code'] : ''; $example = isset($_GET['example']); $num_of_spaces_to_indent = isset($_POST['add_indent']) ? 4 : 0; $codeFormatted = normalize_indent($code, $num_of_spaces_to_indent); $disabled = ''; $msg = 'PROCESSING COMPLETE (copy from RIGHT)'; $outputBackgroundColor = 'wheat;'; if($code == '') { $disabled = ' disabled="disabled" '; $outputBackgroundColor = 'white;'; $msg = 'Paste code on LEFT. Copy on RIGHT.'; } $infoHideClass = "hide"; $infoHref = "./?example=1"; if($example) { $infoHideClass = ""; $infoHref = "./"; } if(isset($_POST['ajax']) && $_POST['ajax'] == 'Y') { echo json_encode($codeFormatted); exit; } ?> wittman.org - Indent Four Spaces for StackOverflow

Example (+)

Normalizes indented code snippets so the outer block level of indention becomes four spaces.
Below is pseudo code with {TAB} = tab and {SP} = space:

 
IN
{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}foreach(item in collection)
{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}x = item
{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}end foreach
 
OR
{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}foreach(item in collection)
{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}y = item
{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}{SP}end foreach
 
OUT
{SP}{SP}{SP}{SP}foreach(item in collection)
{SP}{SP}{SP}{SP}{TAB}x = item
{SP}{SP}{SP}{SP}end foreach

Code IN

Code OUT Add 4-space Indent

 

History

Indent Four Spaces for StackOverflow Open Source Code:

http://wittman.org/projects/stackoverflowindentfourspaces/stackoverflowIndentFourSpaces.txt
http://gist.github.com/270790
 
The core formatting function is a port/fork of Michael Stum's #C/.NET code

Copyright (C) 2010 Micah Wittman | http://wittman.org/ | stackoverflow.m[a][t]wittman.org | http://stackoverflow.com/users/11181/micahwittman | @micahwittman