I know the indent and new line characters. I know how to force my code to look a certain way by adding and removing \t within functions and strings themselves.
What I would like to know is what's the best way for outputting strings into the final page so that when you "view source" it's nice and pretty. I don't have control of where the code in the page so I can't assume how many tab characters is needed.
Let's take a look at what I'm talking about.
PHP Code:
<?php
$string = <<<EOD
this text
is not indented
once
EOD;
?>
<?=$string?>
<?=$string?>
Would output to:
Code:
this text
is not indented
once
this text
is not indented
once
While I'd like it to be:
Code:
this text
is not indented
once
this text
is not indented
once
I can, and did, create a function to help with formatting.
PHP Code:
<?php
function t($str, $l) {
$t = implode("\t",array_pad(array(), $l++, ''));
return str_replace("\n","\n".$t, $str);
}
?>
and by using that function
PHP Code:
<?=t($string,1)?>
I can have it output the desired amount of tabs in the view.
Code:
this text
is not indented
once
Ideally though, a person wouldn't have to use that function to allow the multi-line string to have the correct indentation.
Bookmarks