SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    SitePoint Evangelist
    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;


  2. #2
    <? echo "Kick me"; ?> petesmc's Avatar
    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;
    }

    ?>
    Since the code is confusing for me, if you told me where to place the code to change points 2 or 3 I'd be able to probably insert them.

  3. #3
    Happy Holidays !! Paul S's Avatar
    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.

  4. #4
    <? echo "Kick me"; ?> petesmc's Avatar
    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

  5. #5
    Happy Holidays !! Paul S's Avatar
    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);
    ?>

  6. #6
    <? echo "Kick me"; ?> petesmc's Avatar
    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($line01) == "{") && (substr($line, -1) == "}")) {
        
    $new_files .= substr_replace($line"{\n<br>"01);
        
    $new_file .= substr_replace($new_files"\n<br>}", -1);
    }
    echo 
    $new_file;
    ?>
    I put extra <br> tags so you can visually see the difference on the page, although viewing source will show you.

    However, I don't know where to place this in the orginal script?

  7. #7
    Happy Holidays !! Paul S's Avatar
    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);
    ?>

  8. #8
    <? echo "Kick me"; ?> petesmc's Avatar
    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?

  9. #9
    Happy Holidays !! Paul S's Avatar
    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)

  10. #10
    SitePoint Evangelist
    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--;
            } 
    which fines the } and increase the tabbing by 1

    and

    PHP Code:

            
    if (strstr($line"{")) {
                
    $tab++;
            } 
    that decrease the tabbing by one.

    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++;
            }
        } 
    is where you are doing it.

    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. 

  11. #11
    SitePoint Evangelist
    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.

  12. #12
    <? echo "Kick me"; ?> petesmc's Avatar
    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"01);
    $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;
    }

    ?>

  13. #13
    SitePoint Evangelist
    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"; } 
    was left unchanged. it was not indented to the normal level though so something is happening....

  14. #14
    <? echo "Kick me"; ?> petesmc's Avatar
    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"; }
    ?>
    I can work on the if($action) {echo "blah"; } , tonight.

    -peter


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •