Page 1 of 2
any progs for applying a certain style to php?
Posted: Fri Jul 22, 2005 12:36 pm
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?
Posted: Fri Jul 22, 2005 2:42 pm
by Burrito
I haven't seen it for php, but I've seen some that do it for HTML.
sounds like a cool project

Posted: Fri Jul 22, 2005 2:49 pm
by pickle
What do you mean "style of code" - do you mean just highlighting, or spacing as well?
Posted: Fri Jul 22, 2005 2:52 pm
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
Posted: Fri Jul 22, 2005 3:05 pm
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

Posted: Fri Jul 22, 2005 3:08 pm
by pickle
Have you looked into using
highlight_string() at all?
Posted: Fri Jul 22, 2005 3:13 pm
by nickvd
Tidy will fix and reformat html code, it may not be what you are thinking about, but it works

Posted: Fri Jul 22, 2005 3:40 pm
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?
Posted: Fri Jul 22, 2005 4:27 pm
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?
Posted: Fri Jul 22, 2005 4:55 pm
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.
Posted: Fri Jul 22, 2005 5:01 pm
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.
Posted: Fri Jul 22, 2005 5:04 pm
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!
Posted: Fri Jul 22, 2005 5:23 pm
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.
Re: any progs for applying a certain style to php?
Posted: Fri Jul 22, 2005 6:35 pm
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.
Posted: Fri Jul 22, 2005 8:17 pm
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