...

View Full Version : How do you indent HTML output by PHP?



Grant Palin
12-02-2003, 06:18 PM
I'm querying a database, and printing results in a table. I'm the kind of person that likes to have tr indented 2 spaces from the table tag, and td indented 2 spaces from tr. What I'm doing right now is:



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


When I do it this way, I get the look I want (in the source).

Is there a way to have PHP indent HTML automatically, rather than me adding the spaces myself?

bcarl314
12-02-2003, 06:52 PM
print "\n"; //creates new line
print "\t"; //creates tab character

so...
print "<tr>";
print "\n\t<td>";
print "\n\t</td>";
print "\n</tr>"

should do it

Dylan Leblanc
12-02-2003, 08:54 PM
There is also HTML Tidy for PHP, but that may be going too far for your needs, http://www.coggeshall.org/tidy.php

On my website I don't care about indenting HTML at all. If you look at the source code of this webpage you will see there are not even line breaks (well, only a bit as required by JavaScript), http://skyscraperpage.com/members/

Grant Palin
12-03-2003, 03:37 AM
Originally posted by Dylan Leblanc
There is also HTML Tidy for PHP, but that may be going too far for your needs, http://www.coggeshall.org/tidy.php

Thanks, but I think that's going too far for my needs;).



http://skyscraperpage.com/members/

Yikes, that's a mess!:eek:

bcarl, I'll try your suggestion.

Grant Palin
12-03-2003, 06:33 AM
All right, the new line character is helpful (so every HTML tag in the output source is on it's own line). When I used the tab character, it indented by 8 spaces...I prefer 2 space indents in my HTML documents, so that's no good. So I'm more or less back to where I was before. But that's okay, since I'm not having to do that indenting too often.

Dylan Leblanc
12-03-2003, 06:50 AM
I think the 8 spaces is a result of the program you are using to view the html document, since tabs are sometimes different widths in different programs. Sometimes there are even settings in the program which you can change the width of the tabs.

I made my webpages without any newlines because html documents don't need them.

raf
12-03-2003, 01:01 PM
On my website I don't care about indenting HTML at all. If you look at the source code of this webpage you will see there are not even line breaks
I'll second that. I've never had any need to bother with my xhtml-sources layout. (But then again, i never make mistakes so i don't need to look at the xhtml-source when debugging ;) )

ReadMe.txt
12-03-2003, 03:42 PM
But in the interests of visibility and readability for bothmyself if i want to change something, and other people who are using my html - i want it to be laid out neatly and structured using a bit of whitespace.

Acecool
12-03-2003, 04:02 PM
highlight_string($string);

It keeps spaces and highlights php etc

bcarl314
12-03-2003, 06:12 PM
Originally posted by Grant Palin
All right, the new line character is helpful (so every HTML tag in the output source is on it's own line). When I used the tab character, it indented by 8 spaces...I prefer 2 space indents in my HTML documents, so that's no good. So I'm more or less back to where I was before. But that's okay, since I'm not having to do that indenting too often.

The number of spaces displayed is directly related to your text editor. Check the configuration of your text editor and set the spaces per tab (or something similar) to 2.

Alternativly, I think you can use \s for a space character. Not sure though

missing-score
12-03-2003, 10:31 PM
yes, you can :)

firepages
12-04-2003, 03:05 PM
or look at herdoc syntax for wysiwyg



<?php

$str = <<<TABLE
<table>
<tr>
<td>$variable</td>
</tr><tr>
<td>{$_SESSION['another_var']}</td>
</tr>
</table>
TABLE;

echo $str;

?>

ReadMe.txt
12-04-2003, 08:38 PM
as i've been using it recently, the normal quotes syntax seems to support newlines as well

so

echo 'hello
';
echo "hello
";
echo "hello\n";


are all equal.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum