Skip to main content

PHP Beautifier

24 Sep 2008 -
I use BBEdit as my editor of choice and there is one thing that is sorely lacking - re-indention (or auto-indention) of code, meaning that tabs and spaces are inserted correctly to make the code flow in a nice way.
There is, however, a nice PHP library that does this automatically, called PHP Beautifier (which requires the PEAR package as well) and to automate this in BBEdit, use this script:
error_reporting(E_ALL|E_STRICT);
require_once ('PHP/Beautifier.php');
require_once ('PHP/Beautifier/Batch.php');
if ($argv[1]) {
  try {
    $beauty = new PHP_Beautifier();
    $batch = new PHP_Beautifier_Batch($beauty);
    $batch->addFilter('ArrayNested');
    $batch->setIndentChar("  ");
    $batch->setIndentNumber(1);
    $batch->addFilter('IndentStyles', array('style' => 'k&r'));
    $batch->setInputFile($argv[1]);
    $batch->process();
    print trim($batch->get());
  }
  catch(Exception $exp) {
    echo ($exp);
  }
}

What it does is parse $argv[1] and re-indents the code following given rules, in this case "k&r", which stands for "Kernighan & Ritchie" and is a style that looks like this:

if (cond) { # do... }

It's also set to indent with one tab character, and then you can choose whether you want to use the filter "ArrayNested" which makes this array declaration:

$apa= array("utter", "giraff", "elefant", "ostrich");

Look like this:

$apa= array(
  "utter",
  "giraff",
  "elefant",
  "ostrich"
);

But be warned, it also makes this:

if(in_array($apa, array("one", "two", "three"))){
  # do stuff...
}

Into this:

if(in_array($apa, array(
  "one",
  "two",
  "three"
))) {
  # do stuff...
}
Which can be messy, decide for yourself what you want to use

BBEdit?

To insert this as a filter in BBEdit, save the above filter in BBEdit's script folder (in the menu #!->Unix Filters->Open filters folder) and then open the filter window palette (Windows->Palettes->Unix filters) and setup a shortcut key for the script. Then you're done! BBEdit will send along the current selection or the whole document (if no selection is active) in $argv
Mer i Webblog
Absolut Antique  Absolut Death Star
Hur vljer man publiceringsverktyg fr webben
Sandman recenserar Crysis Warhead
PHP Beautifier
Ny dammsugare
Forum p sandmannet
Nutid dtid hur symbolsprket frndras
Ostkupa
Stranger Things, säsong 5
Första december!
Nordic: The Musical
Livets träd
Myst Book
Web versions of the journals
Predator: Badlands
Myst Book
Selenitic Age
Ny kamera, gammal glöd
Tekoppen
Årets Halloweenfest var mycket lyckad!
Porträtt av t-o-m-u-s-a
Porträtt av u/arielgirle
Profilporträtt
GameConnect
Ny PC!
50 år
Hemma Bäst
Garageuppfart för husbil: Uppfarten är klar!
Ny kamera: Nikon Zf
Hemma Bäst
Garageuppfart för husbil: Massa grus!
The lightsabers are done!
Hemma Bäst
Garageuppfart för husbil: Lagt ut plattor och skyfflat makadam
Hemma Bäst
Garageuppfart för husbil: Grävt och klart!
Hemma Bäst
Pooltak
Nordic: The Musical
Valkyrior
Tekoppen
Kräftskiva och eldfest på Tekoppen!
Lord of the Rings timeline
Alien Timeline
Borta Bra
Snart är det Medeltidsveckan!
Hemma Bäst
Utebar: Pergola
Hemma Bäst
Utebar: Refrigerator and bar stools
Hemma Bäst
Utebar: Cupboard doors
Nordic: The Musical
Lokes Vrede
Hemma Bäst
Garageuppfart för husbil: Garageuppfart för husbil!
Arkad- och flipperkabinett
Robotar
Nordic: The Musical
Blodsbröder
Nordic: The Musical
Midgård
Nordic: The Musical
Himlen brinner
Nordic: The Musical
Orosmoln i Asgård
Nordic: The Musical
Gudarnas spel
Nordic: The Musical
Oändlig kärlek
Update on the lightsaber project
Hemma Bäst
Skåp till Ute-TV
Thåström i Globen
Bröllopspresent
Födelsedagspresent
Borta Bra
Recension: Jacy's
Thåström
Nordic: The Musical
Kunskapens pris
Nordic: The Musical
Allting börjar alltid någonstans
Borta Bra
Recension: Bohusgården Hotell & Konferens
Borta Bra
Recension: Elite Palace