SitePoint Sponsor |
|
User Tag List
Results 1 to 14 of 14
Thread: a script to auto tab a php file
-
Oct 6, 2001, 11:20 #1
- Join Date
- Feb 2000
- Location
- England
- Posts
- 568
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
a script to auto tab a php file
Some of my files are tabbed badly or not at all so thought it would be good to find a way to auto tab them. I wrote a script to do it. It does the following:
1) Opens all the files one by one in a directory that are .php
2) Remvoes all whitespace and then adds appropriate amount of tabs based on { and }
3) It does not add tabs to lines with //// or #### in (i did not want my longer comments tabbed but did my // comments that go at the end of the line.
BUGS:
1) If you have { or } in use on a line (for example in a regex) this won't work. It needs to be modified to check only the first letter and last letter of a line really.
2) A way to remove if ($action == "do") { echo "do"; } and convert it to multiple lines would be good.
3) Convert if ($action == "do") echo "do";
would also be useful but harder i guess.
4) It only does one directory, going recurisvly through all directories would be really good.
p.s. the formatting of this script is wrong because it suffers from bug 1 and gets updated while running.
BACKUP YOUR FILES BEFORE RUNNING!
PHP Code:
$dir = opendir(".");
while ($file = readdir($dir)) {
if (strstr($file, ".php")) {
$fcontents = file($file);
while (list ($line_num, $line) = each ($fcontents)) {
if (strstr($line, "}")) {
$tab--;
}
if (!((strstr($line, "\\\\")) OR (strstr($line, "####")))) {
$new_file .= growstring("\t", $tab);
}
$new_file .= trim($line) . "\r\n";
if (strstr($line, "{")) {
$tab++;
}
}
$file2 = fopen($file, "w");
fputs($file2, $new_file);
unset($new_file);
unset($tab);
}
}
function growstring($string, $number) {
for ($idx = 0; $idx < $number; ++$idx) {
$newstring .= $string;
}
return $newstring;
}
-
Oct 6, 2001, 16:03 #2
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
Very nice script! Made one change so far, change \r\n to \n:
PHP Code:<?php
$dir = opendir(".");
while ($file = readdir($dir)) {
if (strstr($file, ".php")) {
$fcontents = file($file);
while (list ($line_num, $line) = each ($fcontents)) {
if (strstr($line, "}")) {
$tab--;
}
if (!((strstr($line, "\\")) OR (strstr($line, "####")))) {
$new_file .= growstring("\t", $tab);
}
$new_file .= trim($line) . "\n";
if (strstr($line, "{")) {
$tab++;
}
}
$file2 = fopen($file, "w");
fputs($file2, $new_file);
unset($new_file);
unset($tab);
}
}
function growstring($string, $number) {
for ($idx = 0; $idx < $number; ++$idx) {
$newstring .= $string;
}
return $newstring;
}
?>
-
Oct 6, 2001, 16:21 #3
- Join Date
- Mar 2001
- Location
- Mexico
- Posts
- 1,287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hey I guess it would be more easy (and faster) if you try with regular expressions.
-
Oct 6, 2001, 16:28 #4
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've gotten most of it, just how to change the first and last character to whatever. Anyone know the function?
-Peter
-
Oct 6, 2001, 16:45 #5
- Join Date
- Mar 2001
- Location
- Mexico
- Posts
- 1,287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
something like this?
Code:<?php $str = "this is a test line!"; echo "$str\n"; echo preg_replace("/^(.)(.*)(.)$/",'T'."\\2".'.',$str); ?>
-
Oct 6, 2001, 17:05 #6
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
I've done this:
PHP Code:<?php
$line = "{ echo 1; }";
if ((substr($line, 0, 1) == "{") && (substr($line, -1) == "}")) {
$new_files .= substr_replace($line, "{\n<br>", 0, 1);
$new_file .= substr_replace($new_files, "\n<br>}", -1);
}
echo $new_file;
?>
However, I don't know where to place this in the orginal script?
-
Oct 6, 2001, 17:11 #7
- Join Date
- Mar 2001
- Location
- Mexico
- Posts
- 1,287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Peter, try this
Code:<?php $line = "{ echo 1; }"; echo "$line\n<br>"; $firstchar = "{\n<br>"; $lastchar = "\n<br>}"; echo preg_replace("/^(.)(.*)(.)$/",$firstchar."\\2".$lastchar,$str); ?>
-
Oct 6, 2001, 17:32 #8
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Works on it's own, but not when combines with the script, strangely. Any ideas?
-
Oct 6, 2001, 17:38 #9
- Join Date
- Mar 2001
- Location
- Mexico
- Posts
- 1,287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Peter, why don't you start a new thread? (sorry padders for changing the subject)
-
Oct 6, 2001, 18:56 #10
- Join Date
- Feb 2000
- Location
- England
- Posts
- 568
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The bits that need changing.
To fix bug 1:
PHP Code:if (strstr($line, "}")) {
$tab--;
}
and
PHP Code:
if (strstr($line, "{")) {
$tab++;
}
The script is cycling through line by line so it is checking for the } or { and changing the tab setting for that.
I tried to do a regex on it but I don't like them and what i tried was not working.
To fix the others is not easy because you have to insert lines.
PHP Code:}
if (!((strstr($line, "\")) OR (strstr($line, "####")))) {
$new_file .= growstring("\t", $tab);
}
$new_file .= trim($line) . "\n";
if (strstr($line, "{")) {
$tab++;
}
}
I would guess the logic would be:
1) do the regex to match if ($action == "do") { echo "do"; }
2) Split it so that you have
if ($action == "do") { as one line
echo "do"; as another
and } as another
the change the code to:
PHP Code:
if (our_new_regex) {
$new_file .= growstring("\t", $tab);
$new_file .= "first part of array from regex\n";
$tab++;
$new_file .= growstring("\t", $tab);
$new_file .= "2nd part of regex\n";
$tab--;
$new_file .= growstring("\t", $tab);
$new_file .= "3rd part of regex\n";
} else {
if (!((strstr($line, "\")) OR (strstr($line, "####")))) {
$new_file .= growstring("\t", $tab);
}
$new_file .= trim($line) . "\n";
if (strstr($line, "{")) {
$tab++;
}
}
} // end bracket from new processing just added.
-
Oct 6, 2001, 18:59 #11
- Join Date
- Feb 2000
- Location
- England
- Posts
- 568
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
[QUOTE]Originally posted by petesmc
[B]Hi,
Very nice script! Made one change so far, change \r\n to \n:
QUOTE]
that is what i was using inititally but my text editor on windows did not like it and was putting boxes instead of line breaks. Windows exporer showed it fine when viewing source but the text editor wanted \r\n for some reason.
-
Oct 6, 2001, 19:18 #12
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Here is some new code that changes:
{ code }
to
{
code
}
CHeck if it works:
PHP Code:<?php
$dir = opendir(".");
while ($file = readdir($dir)) {
if (strstr($file, ".php")) {
$fcontents = file($file);
while (list ($line_num, $line) = each ($fcontents)) {
if (strstr($line, "}")) {
$tab--;
}
if (!((strstr($line, "\\")) OR (strstr($line, "####")))) {
$new_file .= growstring("\t", $tab);
}
$a = substr($line,0,1);
$b = substr($line,-3);
$c = substr($line,-2);
if(($a == "{") AND (($b == "}\r\n") OR ($c == "}\n"))) {
if($b == "}\r\n") {
$d = -3;
} else {
$d = -2;
}
$new_lines = substr_replace($line, "{\n", 0, 1);
$new_lines = substr_replace($new_lines, "\n}\n", $d,-1);
echo $new_lines;
$new_file .= trim($new_lines);
unset($new_lines);
} else {
$new_file .= trim($line) . "\n";
}
if (strstr($line, "{")) {
$tab++;
}
}
$file2 = fopen($file, "w");
fputs($file2, $new_file);
unset($new_file);
unset($tab);
}
}
function growstring($string, $number) {
for ($idx = 0; $idx < $number; ++$idx) {
$newstring .= $string;
}
return $newstring;
}
?>
-
Oct 6, 2001, 19:45 #13
- Join Date
- Feb 2000
- Location
- England
- Posts
- 568
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
not for me
PHP Code:if ($rabit == "orange") { echo "true"; }
-
Oct 7, 2001, 05:08 #14
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
My addition is the one the changes:
PHP Code:<?php
if($action)
{ echo "blah"; }
?>
-peter
Bookmarks