Jump to content

Advanced PHP Script: Code Design

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Takumi

Takumi

    Newbie

  • Members
  • PipPip
  • 16 posts
Hi I am currently building a script for Code Design. The programming is done by multiple colleagues, which all use a different 'design' for its script. Making this script would save me a lot of time.

Code design:
\n = newline

\t = tab

  = space


<?php\n\tif (example) {\n\t\techo "Hello World";\n\t}\n?>


This will output:


<?php

	if (example) {

		echo "Hello World";

	}

?>

This is my plan on making the script.

Quote

Step 1: Remove <?php and ?> (will be added later back)
Step 2: Divide the script into lines and add this to an array. $lines = array($linenumber, $linecontent, $linetype);
Example:
$lines = array(array(1, "text", "6"), array(2, "text2", "4.2"));
All types that needs to be on different lines:
0 Do not edit '/*' till '*/'. So add this to the array as new line(s).
1 When '//' till '\n' (new line) is found add this to the array as new line.
2 When ';' is found in the script, add everything between previous line and ';' to the array as a new line.
3 When 'if' till '{' must be on a new line.
4.1 When '}' and ('else' or 'elseif') and '{' is found add this to the array as new line.
4.2 Else when '}' is found, add this to the array as new line.
5 When 'foreach' till '{' is found, add this to the array as new line.
6 When 'for' till '{' is found, add this to the array as new line.
7 When 'while' till '{' is found, add this to the array as new line.
8 When 'for' till '{' is found, add this to the array as new line.

Step 3: Remove all whitespace from the array except type 0
Step 4: Add spaces and tabs according to the type:
Types with new line: 1,2
Types with new line and extra tab: 3,5,6,7,8
Types with new line and one tab less: 4.1, 4.2

Step 5: implode the script back together.
Step 6: Add <?php on the first line and ?> on the last line.

Do you think this is possible, and what is your opinion about this?

Takumi

Edited by Takumi, 08 November 2010 - 07:24 AM.


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Of course it's possible, but what's the reason?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Takumi

Takumi

    Newbie

  • Members
  • PipPip
  • 16 posts
Colleagues of me are working sometimes very unclear and disorganized. I will have to change the code all by myself. If I make a script for it, I wont have to do it no more in the future

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
You wan't to reorganize the code structure so it have a better readability? There are plenty of these scripts out there, they are called Code Beautifiers or Code Formatters
a quick googling on code beautifier php gives me some nice hits that seems to be good for this. Also, in the php IDE I use, phpDesigner, there is one built in :-)
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Zend Studio has this built in as well, and you are able to specify parameters.

#6
Takumi

Takumi

    Newbie

  • Members
  • PipPip
  • 16 posts
Thank you very much, that is exactly what im looking for!
Do you have also a free solution, I need to pay for phpDesigner .

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Yes, there were free scripts as well when I googled on "code beautifier php" if I remember right. I'm not sure the code beautifyer is in the free version of php designer, but can be found here PHP Designer 2007 - Personal - Free software downloads and software reviews - CNET Download.com if you want to check it out.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
nomaden

nomaden

    Newbie

  • Members
  • PipPip
  • 10 posts
I am using netbeans 6.9.1 for PHP development and it have built in code formatter. We could customize how the code should be formatted such as new lines after a function, etc. The setting also could be exported and imported if in case you want to share how the code should be formatted to your colleagues. The only downside there's no multiple file formatter, only the active document could be formatted :(.