any progs for applying a certain style to php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

any progs for applying a certain style to php?

Post by Todd_Z »

for example:

Code: Select all

<?
function funky()    {
         $foo =     'bar';
$fu='bar';
    return  $fu  . $foo;
}?>
into:
<?php
  function funky ( ) {
    $foo = "bar";
    $fu = "bar";
    return $fu.$foo;
  }
?>
Has anyone seen a program/script that takes a file as input and outputs a user-specified style of php code?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I haven't seen it for php, but I've seen some that do it for HTML.

sounds like a cool project :P
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What do you mean "style of code" - do you mean just highlighting, or spacing as well?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

single quotes vs doulbe quotes spaces between the concat etc...

I am trying to rmember the name of the conversion utility that did it for HTML tag attributes but it is escaping me at the moment....grr
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You mean "tidy"?

I remember there is a tool "indent" that does the same to sourcecode ;)
Probably there are other "code beautifiers" out there too :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Have you looked into using highlight_string() at all?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Tidy will fix and reformat html code, it may not be what you are thinking about, but it works :)
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

There is some PHP indenting script for Vim at http://www.vim.org/scripts/script.php?script_id=1120 and in that script there are some goovy functions that do magical stuff. Maybe that can be modifed for personal use?
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Well here's where im going with this... I'm planning to start a code bank site (another one of those?!?! Anywho - so I was hoping that when someone uploads a script, it could get re-formatted into a style that is the same for all the scripts on the site. Any ideas?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

sounds like a regular expression nightmare I had a few nights ago....

couple problems I could see:

1) how do you determine what is the "correct" way to format the php? Maybe there could be an option to have it formatted by the users choice and all the scripts they want would then be formatted accordingly...that'd be cool
2) you're going to have to take into account an immeasurable amount of different styles. Just today for example in another thread we had a 2 page discussion on whether or not echo statements and variable declaraions should be multilined or not... I think you'd have a very difficult time accounting for everyone's different style and coding preference.

I think it's a fantastic idea and I'd love to see you go for it, but I think it'd be a tremendous amount of work and honestly I'm not sure the reward (end result) would be worth it.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I'm hoping you mean by styles that you mean appearance of code, not actual coding guidlines. If it's the former, then start by stripping all whitespace. Then, put a newline character after every '{', and indent every line until you get to a '}'. Syntax highlighting can easily be done by highlight_string().

If you truly do mean the latter, then I agree with ~Burrito - not worth the hassle.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Absolutely correct, a whole lot of work, but it would be nice to have all the files uploaded be the same format. It was be a simple format so that anyone would be able to read the code quickly.
Burrito wrote:how do you determine what is the "correct" way to format the php?
My way!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Making all the code uploaded follow your guidlines is impossible (and I don't use that term lightly). There are an infinite number of ways to do pretty much anything. Writing code to parse and conform any uploaded file would also need to be an infinite function.

I'd focus on making the spacing and highlighting work properly. If you want code conformity, just work in an approval process where you have to double check it before it gets posted.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: any progs for applying a certain style to php?

Post by Roja »

Todd_Z wrote: Has anyone seen a program/script that takes a file as input and outputs a user-specified style of php code?
http://www.bierkandt.org/beautify/
pickle wrote:Making all the code uploaded follow your guidlines is impossible (and I don't use that term lightly). There are an infinite number of ways to do pretty much anything. Writing code to parse and conform any uploaded file would also need to be an infinite function.
"You should never, never doubt what nobody is sure about" - Willy Wonka.
Last edited by Roja on Mon Jul 25, 2005 10:14 am, edited 1 time in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If you wanted to build something like this yourself, use PHP's tokenizer functions:

http://us2.php.net/manual/en/ref.tokenizer.php
Post Reply