2 //Downloaded from http://shadsplace.org/beautify-php/beautify.phps
4 function publicProcessHandler($str, $indent)
6 // placeholders prevent strings and comments from being processed
7 preg_match_all("/\/\*.*?\*\/|\/\/[^\n]*|#[^\n]|([\"'])[^\\\\]*?(\\\\.[^\\\\]*?)*?\\1/s", $str, $matches);
8 $matches[0]=array_values(array_unique($matches[0]));
9 for ($i=0;$i<count($matches[0]);$i++
){
10 $patterns[]="/".preg_quote($matches[0][$i], '/')."/";
11 $placeholders[]="'placeholder$i'";
12 // double backslashes must be escaped if we want to use them in the replacement argument
13 $matches[0][$i]=str_replace('\\\\', '\\\\\\\\', $matches[0][$i]);
17 $str=preg_replace($patterns, $placeholders, $str);
20 //parsing and indenting
21 $str=privateIndentParsedString(privateParseString($str), $indent);
23 // insert original strings and comments
24 for ($i=count($placeholders)-1;$i>=0;$i--){
25 $placeholders[$i]="/".$placeholders[$i]."/";
29 $str=preg_replace($placeholders, $matches[0], $str);
34 function privateParseString($str)
36 // inserting missing braces (does only match up to 2 nested parenthesis)
37 $str=preg_replace("/(if|for|while|switch)\s*(\([^()]*(\([^()]*\)[^()]*)*\))([^{;]*;)/i", "\\1 \\2 {\\4\n}", $str);
38 // missing braces for else statements
39 $str=preg_replace("/(else)\s*([^{;]*;)/i", "\\1 {\\2\n}", $str);
42 $str=preg_replace("/([;{}]|case\s[^:]+:)\n?/i", "\\1\n", $str);
43 $str=preg_replace("/^function\s+([^\n]+){/mi", "function \\1\n{", $str);
45 // remove inserted line breaks at else and for statements
46 $str=preg_replace("/}\s*else\s*/m", "} else ", $str);
47 $str=preg_replace("/(for\s*\()([^;]+;)(\s*)([^;]+;)(\s*)/mi", "\\1\\2 \\4 ", $str);
49 // remove spaces between function call and parenthesis and start of argument list
50 $str=preg_replace("/(\w+)\s*\(\s*/", "\\1(", $str);
52 // remove line breaks between condition and brace,
53 // set one space between control keyword and condition
54 $str=preg_replace("/(if|for|while|switch)\s*(\([^{]+\))\s*{/i", "\\1 \\2 {", $str);
58 function privateIndentParsedString($str, $indent)
60 $count = substr_count($str, '}')-substr_count($str, '{');
65 $strarray=explode("\n", $str);
67 for($i=0;$i<count($strarray);$i++
){
68 $strarray[$i]=trim($strarray[$i]);
69 if (strstr($strarray[$i], '}')){
72 if (preg_match("/^case\s/i", $strarray[$i])){
73 $level=str_repeat("\t", $indent*($count-1));
74 } else if (preg_match("/^or\s/i", $strarray[$i])){
75 $level=str_repeat("\t", $indent*($count+
1));
77 $level=str_repeat("\t", $indent*$count);
79 $strarray[$i]=$level.$strarray[$i];
80 if (strstr($strarray[$i], '{')){
84 $parsedstr=implode("\n", $strarray);
89 if(substr($argv[1],-3) == 'php')
90 $strarray = file($argv[1]);
92 $str = file_get_contents('php://stdin');
93 $strarray = explode("\n",$str);
95 for($i=0;$i<count($strarray);$i++
){
96 $strarray[$i]=trim($strarray[$i]);
98 $str=implode("\n", $strarray);
99 $str=publicProcessHandler($str, $indent);
This page took 0.339964 seconds and 4 git commands to generate.