Is there a script which cleans up source code?

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

Post Reply
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Is there a script which cleans up source code?

Post by insight »

I was just wondering if there is a script which cleans up source code. I've noticed a lot of websites are able to do it but I'm unsure how.

Example:

From my clan site
From
Image
To
Image

My Site

From
Image
To
Image

I am fully aware that adding "\n" will move the lines to a new line (which I will be doing shortly) but what I don't know how to do is organize them.

<table>
---<tr>
------<td>Hello</td>
---</tr>
</table>

Instead of

<table>
<tr>
<td>Hello</td>
</tr>
</table>

Is there some sort of code which does this?
UnknownOne
Forum Newbie
Posts: 5
Joined: Thu Jul 23, 2009 8:52 am

Re: Is there a script which cleans up source code?

Post by UnknownOne »

I actually use a site to clean up a few html pages I use/save when browsing.

Code: Select all

http://www.iwebtool.com/html_optimizer
It's pretty awesome. You can also look for "tiny" or something. I've completely forgotten what it's called but I'm sure it's "tiny" html optimizer.
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: Is there a script which cleans up source code?

Post by insight »

UnknownOne wrote:I actually use a site to clean up a few html pages I use/save when browsing.

Code: Select all

http://www.iwebtool.com/html_optimizer
It's pretty awesome. You can also look for "tiny" or something. I've completely forgotten what it's called but I'm sure it's "tiny" html optimizer.
Yea I'm fully aware of sites like this. I myself normally use W3C Validator.. What I'm wondering is, is there a way to do it automatically? Like in the first example above the creator has a messy looking source code like my own, but when it is displayed in the browser it looks clean. Mine doesn't.

Mind you the echo script is EXACTLY what the browser is reading and displaying. I just don't know how users are able to output it cleanly. Mine comes out like sh*t :(
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Is there a script which cleans up source code?

Post by Eran »

What have all the HTML inside the PHP as a string? write HTML as you normally would and spruce up dynamic content where it's relevant. This way it's easier to maintain proper indentation, IDEs can recognize the syntax and there's no memory allocation for the string.
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: Is there a script which cleans up source code?

Post by insight »

pytrin wrote:What have all the HTML inside the PHP as a string? write HTML as you normally would and spruce up dynamic content where it's relevant. This way it's easier to maintain proper indentation, IDEs can recognize the syntax and there's no memory allocation for the string.
Most of my site will likely be dynamic. I'm only in the beginning stages and since I'm relatively new to PHP and SQL I'm writing everything in php and later on will be implementing a lot of changes.

All I was asking was how (in the first example) are they indenting the code when there is no indentation in the php echo?

For example, they can echo:

echo "<table>"
. "<tr>"
. "<td>Welcome</td>"
. "</tr>"
. "</table>";

And get it to look like this when it is outputted to the browser:

<table>
---<tr>
------<td>Welcome</td>
---</tr>
</table>

When I do the exact same it get's outputted to the browser as:

<table>
<tr>
<td>Welcome</td>
</tr>
</table>

I was just wondering how they are able to indent the html code? Is there a script they are using? I tried using this one I found off Google but am unable to get it to work.

Code: Select all

<?php
 
//Function to seperate multiple tags one line
function fix_newlines_for_clean_html($fixthistext)
{
    $fixthistext_array = explode("\n", $fixthistext);
    foreach ($fixthistext_array as $unfixedtextkey => $unfixedtextvalue)
    {
        //Makes sure empty lines are ignores
        if (!preg_match("/^(\s)*$/", $unfixedtextvalue))
        {
            $fixedtextvalue = preg_replace("/>(\s|\t)*</U", ">\n<", $unfixedtextvalue);
            $fixedtext_array[$unfixedtextkey] = $fixedtextvalue;
        }
    }
    return implode("\n", $fixedtext_array);
}
 
function clean_html_code($uncleanhtml)
{
    //Set wanted indentation
    $indent = "    ";
 
 
    //Uses previous function to seperate tags
    $fixed_uncleanhtml = fix_newlines_for_clean_html($uncleanhtml);
    $uncleanhtml_array = explode("\n", $fixed_uncleanhtml);
    //Sets no indentation
    $indentlevel = 0;
    foreach ($uncleanhtml_array as $uncleanhtml_key => $currentuncleanhtml)
    {
        //Removes all indentation
        $currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);
        $currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);
        
        $replaceindent = "";
        
        //Sets the indentation from current indentlevel
        for ($o = 0; $o < $indentlevel; $o++)
        {
            $replaceindent .= $indent;
        }
        
        //If self-closing tag, simply apply indent
        if (preg_match("/<(.+)\/>/", $currentuncleanhtml))
        { 
            $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
        }
        //If doctype declaration, simply apply indent
        else if (preg_match("/<!(.*)>/", $currentuncleanhtml))
        { 
            $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
        }
        //If opening AND closing tag on same line, simply apply indent
        else if (preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && preg_match("/<\/(.*)>/", $currentuncleanhtml))
        { 
            $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
        }
        //If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level
        else if (preg_match("/<\/(.*)>/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml))
        {
            $indentlevel--;
            $replaceindent = "";
            for ($o = 0; $o < $indentlevel; $o++)
            {
                $replaceindent .= $indent;
            }
            
            $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
        }
        //If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level
        else if ((preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml))
        {
            $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
            
            $indentlevel++;
            $replaceindent = "";
            for ($o = 0; $o < $indentlevel; $o++)
            {
                $replaceindent .= $indent;
            }
        }
        else
        //Else, only apply indentation
        {$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;}
    }
    //Return single string seperated by newline
    return implode("\n", $cleanhtml_array); 
}
?>
 
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Is there a script which cleans up source code?

Post by Weirdan »

Post Reply