PHP/HTML Indentation

Associate
Joined
26 Jun 2003
Posts
1,140
Location
North West
If you have a function which echos html such as

PHP:
function bob()
{
    echo '<b>woo</b>';
}

When you look at the HTML source of this result, it is indentated with however many tabs before the echo, making the HTML very untidy.

Is there any settings on apache to stop this? I like nice and tidy html.

Thx
 
Tabs in your PHP source do not appear in the final markup unless you explicitly echo them.

See:

PHP:
<?php
								echo 'LOL';
echo "\n";
echo '							LOL';
?>

Produces:

Code:
LOL
							LOL
 
they are appearing. This function:

Here is a slice:


PHP:
class Framework 
{	
	function printSite()
	{	
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html>
		<head>
		<title>'.$this->settings['websitetitle'].'</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<meta name="keywords" content="'.$this->settings['keywords'].'" />
		<meta name="description" content="'.$this->settings['description'].'" />
		<meta name="author" content="Jonathan Dawber" />
		<link rel="stylesheet" type="text/css" href="style/style.css" />
		</head>
		</html>';		
	}	
}

The actual code is tabbed twice on each line, but I want to keep these tabs to keep the php file tidy but it indents all of the code.

I dont wanna write echo on each line or outdent the code.

See waht i mean?
Anyway around?
 
Last edited:
Aha, so it *is* doing waht robmiller said, becase you have tabs within the echo string there :)

You could run it through a str_replace() and strip out all the \t characters I suppose.
 
Code:
<?php
class Framework 
{    
    function printSite()
    {    
        echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
        . "<html>\n"
        . "<head>\n"
        . "<title>{$this->settings['websitetitle']}</title>\n"
        . "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n"
        . "<meta name=\"keywords\" content=\"{$this->settings['keywords']}\" />\n"
        . "<meta name=\"description\" content=\"{$this->settings['description']}\" />\n"
        . "<meta name=\"author\" content=\"Jonathan Dawber\" />\n"
        . "<link rel=\"stylesheet\" type=\"text/css\" href=\"style/style.css\" />\n"
        . "</head>\n"
        . "</html>";
    }    
}  
?>
is one way to do it 'neatly'
 
You could write everything to a buffer, then chuck it through a quick HTML-tidying function at the end if it's important to you. That's what I do when speed isn't so much of an issue (though we're only talking ~2ms on the largest pages I've tried)... :)

arty
 
What exactly are you building this for anyway? It seems shocking to me that you would hard code html into a php class which means that the server has to do more work.
 
As I said, the code I posted was an example!!!

The body tags are missing because its a "slice", read again :p

I just wanted to know if there was anyway to get rid of the tabs automatically or someway, looks like there isnt. Thx anyway
 
Back
Top Bottom