Perfectionist's code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Perfectionist's code

Post by Todd_Z »

Hello all...

Here's the problem: When i include() files, i dont have those included files formatted to be indented the certain amount that would fit the place that they are being put in, so basically im trying to find a way to include a file but put a leader on each line that would consist of a string of "\t"s. Thanks in advance...
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

are you including php code or just text/html?
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

php code
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Umm, why does this even matter to you? Just make sure each file is formatted the correct way. Because when you include() it, it's not like it permanently links the contents of file B to file A. You are basically just referencing it.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

<?php


ob_start();
include('script.php');
$generated_html = ob_get_contents();
ob_end_clean();

echo str_replace("\r\n", "\r\n\t\t\t", $generated_html);


?>
you may need to replace \r\n w/ just \n or \r depending on what your script.php outputs. but most likely \r\n will work

its kinda resource intensive if its just for cosmetic purposes though...
Last edited by rehfeld on Wed Dec 01, 2004 5:14 pm, edited 1 time in total.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Mmmm title of subject - Perfectionist's code - I like to have purdy coding.... Second -> Obviously its just referencing, but i dont feel like making all those other files having like 14 indentations before the coding...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Todd_Z wrote:Obviously its just referencing, but i dont feel like making all those other files having like 14 indentations before the coding...
You don't get what he's saying I don't think... you will never actually see the 2 files together so it doesnt matter:

file.php

Code: Select all

<?php
include("file2.php");
?>


file2.php

Code: Select all

<?php
echo "hello";
?>

You will never ever see the word echo or the word hello on file.php unless you are useing some php editor that shows the code when you do an include.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Source code in a browser
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Then you are referring to the HTML output, not the php code itself.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Right but since i already tried to find an output source formatter ( No, Tidy is not what i mean ) I hoped that i could do something along the lines of prefixing each line of the include with a string of \t's. Impossible to do?
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
How would you ensure that a special line is indended 3 tabs, and the other one e.g. 5 tabs? Sounds rather unlogic.

If this is so important to you to have a clean HTML structure, you should follow one coding stlye in all your projects.

djot
-
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

guess which library has these options? :P

indent: no, yes or auto
If set to yes, *library* will indent block-level tags. The default is no. If set to auto *library* will decide whether or not to indent the content of tags such as title, h1-h6, li, td, th, or p depending on whether or not the content includes a block-level element. You are advised to avoid setting indent to yes as this can expose layout bugs in some browsers.
indent-spaces: number
Sets the number of spaces to indent content when indentation is enabled. The default is 2 spaces.
indent-attributes: bool
If set to yes, each attribute will begin on a new line. The default is no.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Todd_Z wrote:Right but since i already tried to find an output source formatter ( No, Tidy is not what i mean ) I hoped that i could do something along the lines of prefixing each line of the include with a string of \t's. Impossible to do?
does my example not work?
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Thanks rehfeld - it worked... much appreciation.

Code: Select all

<?php
function cleanInclude ($filename, $indentation) {
	ob_start();
	include($filename);
	$generated_html = ob_get_contents();
	ob_end_clean();
	echo str_replace("\n", "\n".$indentation, $generated_html);
}
?>
Okay, so there's the code...
Second problem - If i have an include inside of an include, this doesn't work (my guess because of the ob and it already being dealt with) Anyways, is there a solution?
Post Reply