I know the indent and new line characters. I know how to force my code to look a certain way by adding and removing \ 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
$string = <<<EOD
this text
is not indented
once
EOD;
?>
<?=$string?>
<?=$string?>
Would output to:
this text
is not indented
once
this text
is not indented
once
While I’d like it to be:
this text
is not indented
once
this text
is not indented
once
I can, and did, create a function to help with formatting.
<?php
function t($str, $l) {
$t = implode("\ ",array_pad(array(), $l++, ''));
return str_replace("\
","\
".$t, $str);
}
?>
and by using that function
<?=t($string,1)?>
I can have it output the desired amount of tabs in the view.
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.