Jump to content

[SOLVED] Echoing with indentation


Xu Wei Jie

Recommended Posts

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?

Link to comment
Share on other sites

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........

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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"; ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

 

 

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.