How can I make a php script add a tab to every line of an include file? How can I make a php script add a tab to every line of an include file? php php

How can I make a php script add a tab to every line of an include file?


In your test.inc file, you can use output buffering to capture all the output of the PHP script, before it is sent to the browser. You can then post-process it to add the tabs you want, and send it on. At the top on the file, add

<?php  ob_start();?>

At the end, add

<?php  $result = ob_get_contents();  ob_end_clean();  print str_replace("\t" . $result, "\n", "\n\t");?>

I don't necessarily subscribe to this solution - it can be memory intensive, depending on your output, and will prevent your include file from sending partial results to the client as it works. You might be better off reformatting the output, or using some form of custom "print" wrapper that tabs things (and use printing of heredocs for constant HTML output).

Edit: Use str_replace, as suggested by comment


I don't think your solution can be done easily. You might consider using HTML Tidy to clean your source code before presenting it to a client. There are good tutorials for it on the internet.


The easiest solution is to add leading tabs to the include file, but instead of using literal tabs, use the \t escape sequence.