Xu Wei Jie Posted March 16, 2009 Share Posted March 16, 2009 I would like to ask when we do echo , is it possible that the text that we echo be correct indented from where the echo is placed at? Supposedly, $str= " This is a text file "; echo $str; will give me This is a text file echo $str; will give me This is a text file Any great ideas on how to do this? Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 The easy solution would be to simply wrap your output in <pre> tags. Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 Actually, Ive re-read your post. Where echo is placed has no effect on the output, that makes no sense. echo simply outputs the arguments it is passed. You need to look into the control chars or using printf. Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 16, 2009 Author Share Posted March 16, 2009 Actually, pre tags won't work because my output is not printed onto a browser. I have thought of printf or print but it is hard to control the indentation because you hardly know where the "output function" (echo or print or can be any other function that outputs something) is indented at by the programmer. Unless I can somehow detect the indentation........ Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 Like I said, where echo occurs within your application has no effect on your output. The entire idea makes no sense. Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 16, 2009 Author Share Posted March 16, 2009 Sorry if have make a senseless comment actually I should have make it clearer. It really does not have much to do with echo or printf. It is more of the indentation of an output function with respect to how the output is indented if that makes it more clear Actually, let me make use of echo (it can be any other output function I have defined) to make an demonstration of what I want to achieve. Hope this is more sensible examples ...spaces here....<?php echo "text is indented here"?> gives us ...spaces here....text is indented here while <?php echo "text is indented here"?> gives us text is indented here Let me state a problem I have right now <?php $str= " I want it to print this way ";?> ...spaces here....<?php echo $str?> gives me ...spaces here....I want it to print this way but instead I want it this way ...spaces here....I want ...spaces here....it to print ...spaces here....this way Does it make more sense now? Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 I understand exactly what you are saying, I am simply stating it makes little sense to think about echo in that way. Again I will say, 'where echo occurs within your application has no effect on your output' You need to rethink your logic because you are barking up the wrong tree. Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 16, 2009 Author Share Posted March 16, 2009 Well, let's not involve echo then How about self defining a print function and I wish to achieve that effect? Quote Link to comment Share on other sites More sharing options...
corbin Posted March 16, 2009 Share Posted March 16, 2009 The only way it would be possible would be to have __LINE__ and __FILE__ passed to the parameter and then open the file, find the line and count the indentions. That's straight up ghetto though. You should have output output how you want it. The spacing of your code should generally have nothing to do with output x.x. Just wondering... Why are you trying to do this? Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 How about self defining a print function and I wish to achieve that effect? Its not going to happen. Not with php anyway. You may be able to hack the php interpreter to do something like this but really, why? Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 16, 2009 Author Share Posted March 16, 2009 I wish to do something like this because indentation and output aesthetics is quite important to me. Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 Then format your output. eg; <?php echo "this is text"; echo " this is indented text"; ?> Output is passed to echo as an argument. It has NOTHING at all do do with where echo appears in your code. Think about what your asking, it makes no sense. Another option is to jump out of php. eg; <?php echo "say hello\n"; ?> here is some indented text <?php echo "say goodbye"; ?> Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 16, 2009 Author Share Posted March 16, 2009 Corbin, how would you find a line and count indentations? If I can get hold of how much a line of code is indented, I might be able to use that information. Thorpe, I understand that it does not make sense for output to have the same indentation as the output function. In a programmer's perspective, output if need to be indented should already been done so and has no correlation with the output function. But in a special case of mine, I really need to tie both together and let them have the same indentation to achieve the aesthetics I need. Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 But in a special case of mine, I really need to tie both together and let them have the same indentation to achieve the aesthetics I need. Then you will need to write your own php parser. Good luck. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 16, 2009 Share Posted March 16, 2009 <?php GhettoEcho("Hi\n"); GhettoEcho("Hello\n"); GhettoEcho("It pains me to actually code something this ghetto.\n"); function GhettoEcho($string = "") { $db = debug_backtrace(); $db = $db[0]; --$db['line']; if(empty($db['file'])) trigger_error("The filename from which GhettoEcho was called cannot be determined from debug_backtrace().", E_USER_ERROR); $contents = file($db['file']); if($contents == false) trigger_error("File in which GhettoEcho was called could not be read.", E_USER_ERROR); if(!isset($contents[$db['line']])) trigger_error("The line specified in debug_backtrace() does not appear to exist in the file specified.", E_USER_ERROR); //Note: if GhettoEcho is called in eval(), this will not work. $pos = stripos($contents[$db['line']], 'GhettoEcho'); if($pos !== false) { if($pos === 0) { echo $string; } else { $start = substr($contents[$db['line']], 0, $pos); echo $start . $string; } } return false; //I don't even feel like throwing an error for this one.... Too lazy to C/P and change error now ;p. } Quote Link to comment Share on other sites More sharing options...
laffin Posted March 16, 2009 Share Posted March 16, 2009 why make it so complex function tabecho($string,$indent=0) { static $li=0; if($indent) $li=$indent if($li) echo strrepeat(' ',$indent); echo $string; } tabecho('Hi!',; tabecho('This is even cheesier'); Works just as well Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 why make it so complex function tabecho($string,$indent=0) { static $li=0; if($indent) $li=$indent if($li) echo strrepeat(' ',$indent); echo $string; } tabecho('Hi!',; tabecho('This is even cheesier'); Works just as well Thats not at all what the op was asking. Quote Link to comment Share on other sites More sharing options...
laffin Posted March 16, 2009 Share Posted March 16, 2009 well from there its just a simple file to array call I just understand the question differently. I understand that he wants a predetermined tab size to echo out before the file lines. which ya dun need to read in the whole file for, just prepend a string to each line so even with the above routine, a file to tabecho routine is just a simple function of function tabfile($filename,$indent=0) { $lines=file($filename); if(!empty($lines)) foreach($lines as $line) tabecho($line,$indent); } tabfile('NotherSample.txt'); See not hard at all Quote Link to comment Share on other sites More sharing options...
trq Posted March 17, 2009 Share Posted March 17, 2009 I understand that he wants a predetermined tab size to echo out before the file lines. Then I think your still not understanding. Quote Link to comment Share on other sites More sharing options...
jlhaslip Posted March 17, 2009 Share Posted March 17, 2009 echo "\t"; will put a tab space into the output. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 17, 2009 Share Posted March 17, 2009 echo "\t"; will put a tab space into the output. Also not understanding the question >.<. And Daniel suggested that twice ;p. Quote Link to comment Share on other sites More sharing options...
trq Posted March 17, 2009 Share Posted March 17, 2009 I think the reason no one is understanding what the op wants is because it is a pretty far out request. The op is asking for the output's indentation to be related to how the call to echo is within his script. eg; <?php $s = "this is foo\n"; echo $s; echo $s; echo $s; ?> Would output: [pre] this is foo this is foo this is foo [/pre] i'm not watching this thread anymore as I seriously consider this to be a rediculous request. Quote Link to comment Share on other sites More sharing options...
jlhaslip Posted March 17, 2009 Share Posted March 17, 2009 I'm with you. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 17, 2009 Share Posted March 17, 2009 Yeah, wanting to do it is entirely retarded.... But what ever ;p. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.