Page 1 of 1
Keeping indents
Posted: Wed Jul 13, 2005 1:02 pm
by someberry
Just wondering if it is possible to keep indents for posts, say in a forum, or messaging system using regex?
For example, if someone wanted to indent their test by a couple of spaces, to make it easier to read, but not anywhere else, how would be best to achive this?
Code: Select all
This has no indent.
This has 5 lines indent
This has 10 lines indent.
Thanks,
Someberry.
Posted: Wed Jul 13, 2005 2:51 pm
by Chris Corbyn
Here's a modified version of my tabs2spaces() function...
v1.1.0 now converts remaining spaces to entities if parameter 2 is set true.
Code: Select all
<?php
/*
tabs2spaces() function written by d11wtq
Converts text containing tab characters into space characters only
-- whilst perfectly maintaining the formatting.
Use:
tabs2spaces(string source [, bool entity-output [, int tab-size]])
Last revised: 13th July 2005
v1.1.0
*/
function tabs2spaces($source, $ents=false, $s=4) {
$space = ($ents)? ' ' : ' '; //Space or entity?
$lines = explode("\n", $source); //Array of lines
$mod = array();
foreach ($lines as $l) {
while (false !== $pos = strpos($l, "\t")) { //Remember position 0 equates to false!
$i = substr($l, 0, $pos);
$t = str_repeat($space, ($s - $pos % $s)); //Width of the tab
$e = substr($l, $pos+1);
$l = $i.$t.$e; //Rebuild the line
} //End while
$mod[] = $l;
} //End foreach
$source = str_replace(' ', $space, implode("\n", $mod)); //Modified this line in v1.1.0 to
//to convert all spaces to entities
return $source;
} // tabs2spaces()
?>
So you'll want to run this on your text:
Code: Select all
$modified = tabs2spaces($input_text, 1);
Lemme know if you need any help.
Posted: Wed Jul 13, 2005 7:26 pm
by programmermatt
Sorry, but I could help but wonder why a simple preg_replace wouldn't work here?
Code: Select all
echo preg_replace("/\t/", " ", " This is a test"); //returns ' This is a test'
And of course you could change the replaced with more spaces, the  , or anything else.
Posted: Fri Jul 15, 2005 6:33 pm
by Ambush Commander
This is actually a really silly problem that's been bugging me too.
You see, tabbing is supposed to be handled by CSS. We shouldn't be it.
If they use a real tab (as in \t), then that's easy to handle. If they use spaces, that's a bit harder.
Code: Select all
Is this tabbed?
Is this tabbed? Tabbed more?
This isn't tabbed right but...
Is it a seperate paragraph?
Does this parse to..
Code: Select all
<p class="e;indent"e;>Is this tabbed?</p>
<p class="e;indent"e;>Is this tabbed? Tabbed more?</p>
<p>This isn't tabbed right but...</p>
<p class="e;indent"e;> Is it a seperate paragraph?</p>
or perhaps...
Code: Select all
<p class="e;indent"e;>Is this tabbed?</p>
<p class="e;indent_two"e;>Is this tabbed? Tabbed more?</p>
<p>This isn't tabbed right but...</p>
<p class="e;indent"e;> Is it a seperate paragraph?</p>
Which means you also have to define indent_three and indent_four etc.
It's mind boggling, methinks.
Oh wait, you people use <BR>s, don't you?
Posted: Sat Jul 23, 2005 10:33 pm
by theda
Me, being the lazy ass I am, just use , just because it does the job... Looks like crap though. I'm sure you could be just as lazy and do:
Code: Select all
<?
$tab = " ";
print $tab."text";
?>
Although my logic fails and I notice that it would just print the text instead of actually spacing it out...
Posted: Sun Jul 24, 2005 4:34 am
by timvw
I'll keep using <pre>...
Posted: Sun Jul 24, 2005 8:08 am
by Ambush Commander
Well, <pre> doesn't wrap, which is okay for code, but not so okay for paragraphs with tabs.
Posted: Sun Jul 24, 2005 7:24 pm
by timvw
I found a similar function...
http://aidan.dotgeek.org/lib/?file=func ... 2space.php
But a CSS solution might be nice too

Posted: Sun Jul 24, 2005 7:26 pm
by Ambush Commander
Parsing and adding inline CSS... hmm... it's not that difficult to do. It's just when you add HTML parsing, DocType compliance, and the whole kaboodle, it gets really hard to do.