Keeping indents

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Keeping indents

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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)? '&nbsp;' : ' '; //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.
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post 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 &nbsp, or anything else.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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=&quote;indent&quote;>Is this tabbed?</p>
<p class=&quote;indent&quote;>Is this tabbed? Tabbed more?</p>
<p>This isn't tabbed right but...</p>
<p class=&quote;indent&quote;> Is it a seperate paragraph?</p>
or perhaps...

Code: Select all

<p class=&quote;indent&quote;>Is this tabbed?</p>
<p class=&quote;indent_two&quote;>Is this tabbed? Tabbed more?</p>
<p>This isn't tabbed right but...</p>
<p class=&quote;indent&quote;> 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?
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Me, being the lazy ass I am, just use &nbsp;, 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 = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
print $tab."text";
?>
Although my logic fails and I notice that it would just print the text &nbsp; instead of actually spacing it out...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I'll keep using <pre>...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, <pre> doesn't wrap, which is okay for code, but not so okay for paragraphs with tabs.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I found a similar function...
http://aidan.dotgeek.org/lib/?file=func ... 2space.php


But a CSS solution might be nice too ;)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

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