-1

guys I have this enormous index.php file which is full of commented out code like:

/*

some code

*/

Also the indents are just horrible and inconsequent.

Is there anyway I can tidy up this code, i.e. remove the commented code pieces and fix the indents to some degree?

user229044
  • 232,980
  • 40
  • 330
  • 338
Gerben
  • 61
  • 7
  • 8
    Can't you simply delete the comments? – Scott Jan 04 '12 at 19:07
  • Yes, there is. By hand. Or figure out how to make your editor do it for you. This is off-topic at Stack Overflow though. – user229044 Jan 04 '12 at 19:07
  • Netbeans, alt + shift + f. Vim = Beautifier.. mm there's probably other ways too. The best way is to not right poor code. – Layke Jan 04 '12 at 19:08
  • similar: http://stackoverflow.com/questions/956911/clean-up-php-html-pages –  Jan 04 '12 at 19:08
  • 1
    You are ruining code by deleting comments. – Dejan Marjanović Jan 04 '12 at 19:09
  • From the sound of it the file needs other maintenance as well; since you probably need to go through it anyway, you can just fix everything as you go, including unneeded comments and indentation. – JJJ Jan 04 '12 at 19:09
  • 3
    @meagar: processing a text file to remove certain features is definitely an on-topic programming question. – nico Jan 04 '12 at 19:09
  • 4
    @webarto: commented code != comments – nico Jan 04 '12 at 19:10
  • 4
    The real problem IMO is `enormous index.php file` – Wesley Murch Jan 04 '12 at 19:11
  • @nico, just reminds me when I open someones 1500 line code with no comments. – Dejan Marjanović Jan 04 '12 at 19:12
  • 3
    @webarto: How does deleting commented-out code ruin actual code? If code shouldn't be there, delete it. Let deprecated code live in the source control history, not in the production code. Also, in my experience, most comments are lies (or at least lies waiting to happen). – David Jan 04 '12 at 19:13
  • @David: I think webarto mistook "commented out code" for "comments", I did the same thing at first. Agree with "You are ruining code by deleting comments" but commenting out unused code instead of removing it is another thing. – Wesley Murch Jan 04 '12 at 19:15
  • Yeah, sorry 'bout that. Me dumb. – Dejan Marjanović Jan 04 '12 at 19:17
  • @nico If that were the question, then I'd agree. But that is not the question. – user229044 Jan 04 '12 at 19:18
  • How big is this index.php? Anwyway, many IDEs (hell, even Notepad++ has a plugin that allows for code beautifying and regexp search/replace) have features for clearing your code. So take yourself some time and start deleting! If the problem turns out to be spaghetti code, well..time for some refactoring, then – Damien Pirsy Jan 04 '12 at 19:21
  • I usually put certain code between /* and */ if I don't need it at the moment or think it'll get handy some day. My php program is done however so I don't need al those commented out code anymore, but I got like 60+ chunks of commented out code... there must be some way to get rid of them... – Gerben Jan 04 '12 at 19:23

3 Answers3

4

Usually a good IDE (Netbeans, Eclipse, most other IDEs...) has a code formatter feature. It just takes a click like CTRL + SHITF +F in Netbeans to indent your code blocks nicely Just use it to format. As for commented out code, you just have to remove it by hand.

Dmitri Snytkine
  • 1,096
  • 1
  • 8
  • 14
0

If all else fails, use PHP... I used this test file http://pastebin.com/VJFzUWHM This will probably fail in some cases, but worth a shot. Here is the result file http://pastebin.com/1rTYtfta

<?php

$string = file_get_contents('php.txt');

$string = preg_replace('#\/\*(.*?)\*\/#is', '', $string); # remove forward slash-asterisk asterisk-forward slash comments
$string = preg_replace('#(\#|\/\/)([^\n]+)#is', '', $string); # remove hash and 2x forward slash comments

echo $string;

?>
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
0

Try this tool, posted to my own web site, but not updated for years. not sure if it'll help, but lots of people still use it.

http://thephppro.com/tools/beautify.php

Tim G
  • 1,812
  • 12
  • 25