<?php
/*
class TabText
@author Andreas Martens
@version 0.1 - expecting further versions
class to create simultanious tabbed text for each column.
Text should be formated with tabs and breaks like this:
Field 1 \t Field 2 \t Field 3 \n
Item 11\t Item 12 \ Item 13\n
Item 21\t Item 22 \t Item 23\n
*/
class TabText {
var $tab = "\t";
var $nl = "\n";
var $w = " ";
var $full_text;
var $fields_maxlen;
var $text_fields;
var $line_fields;
var $tabsize;
var $right;
/*
public method text_format()
expects one var, possible are two more vars for the tabsize and rightbound field (array).
@param string $text should be with tabs and breaks
@param int $tabsize size of a tab, depending on editor or email client
@param array $right array with field numbers, you want to rightbound (count from 0)
*/
function text_format($text,$tabsize=6,$right){
$this->tabsize = $tabsize;
$this->right = $right;
$text_lines = explode("\n",$text);
for($i=0;$i<count($text_lines);$i++){
$this->text_fields[] = explode("\t",trim($text_lines[$i]));
}
$this->max_length();
}
function max_length(){
for($i=0;$i<count($this->text_fields);$i++){
for($e=0;$e<count($this->text_fields[$i]);$e++){
if(strlen($this->text_fields[$i][$e])>$this->fields_maxlen[$e]) $this->fields_maxlen[$e] = strlen($this->text_fields[$i][$e]);
}
}
for($e=0;$e<count($this->fields_maxlen);$e++){
$this->fields_maxlen[$e] = ceil($this->fields_maxlen[$e] / $this->tabsize) * $this->tabsize;
}
}
/*
public method text_line()
adds a line after given line number. You can choose the line art as second parameter.
@param int the line number, you wanna add the line after
@param string just a char, you wanna have the line of
*/
function text_line($line_number,$line_art="-") {
$len = 0;
reset($this->fields_maxlen);
while (list($k, $v) = each ($this->fields_maxlen)) {
$len = $len + $v;
}
reset($this->fields_maxlen);
for($i=0;$i<$len;$i++) {
$this->line_fields[$line_number-1] .= $line_art;
}
$this->line_fields[$line_number-1] .= "\n";
}
/*
public method text_output()
just call to output the whole stuff.
*/
function text_output(){
$this->full_text = $this->line_fields[-1];
for($i=0;$i<count($this->text_fields);$i++){
for($e=0;$e<count($this->fields_maxlen);$e++){
#$this->fields_maxlen[$e] = ceil($this->fields_maxlen[$e] / $this->tabsize) * $this->tabsize;
if(in_array($e,$this->right)){
$tempstring = $this->text_fields[$i][$e];
for($q=0;$q<($this->fields_maxlen[$e]-strlen($tempstring)-1);$q++){
$this->text_fields[$i][$e] = " ".$this->text_fields[$i][$e];
}
$this->text_fields[$i][$e] .= " ";
}
$new_fields[$i][$e] = $this->text_fields[$i][$e];
for($r=0;$r<ceil(($this->fields_maxlen[$e]-strlen($this->text_fields[$i][$e]))/$this->tabsize);$r++){
$new_fields[$i][$e] .= "\t";
}
$this->full_text .= $new_fields[$i][$e];
}
$this->full_text .= "\n".$this->line_fields[$i];
}
return $this->full_text;
}
}
?> |