GlisWeb framework
_txt.tools.php
Vai alla documentazione di questo file.
1 <?php
2 
12  // costanti
13  define( 'REPORT_WIDTH' , 80 );
14 
20  function txtHeader( $t, $c = '=', $w = REPORT_WIDTH ) {
21 
22  $t = strtoupper( $t );
23 
24  $t .= PHP_EOL . txtDateTimeLine( $c );
25 
26  return $t;
27 
28  }
29 
30  function txtDateTime( $f = 'Y-m-d H:i:s', $d = NULL ) {
31 
32  return ( $d === NULL ) ? date( $f ) : date( $f, $d );
33 
34  }
35 
36  function txtLine( $c = '-', $w = REPORT_WIDTH ) {
37 
38  return str_repeat( $c, $w );
39 
40  }
41 
42  function txtFullLine( $c = '-', $w = REPORT_WIDTH ) {
43 
44  return txtLine( $c, $w ) . PHP_EOL;
45 
46  }
47 
48  function txtDateTimeLine( $c = '-', $d = NULL, $w = REPORT_WIDTH ) {
49 
50  $ts = '[ ' . txtDateTime() . ' ]';
51 
52  $w -= strlen( $ts );
53 
54  return txtLine( $c, $w ) . $ts . PHP_EOL;
55 
56  }
57 
58  function txtData( $l, $d, $c = ' ', $w = REPORT_WIDTH ) {
59 
60  $l .= ' ';
61  $d = ' ' . $d;
62  $m = $w - ( strlen( $d ) );
63 
64  if( strlen( $l ) > $m ) {
65  $l = riduciStringa( $l, $m );
66  }
67 
68  $w -= strlen( $l ) + strlen( $d );
69 
70  return $l . txtLine( $c, $w ) . $d . PHP_EOL;
71 
72  }
73 
74  function txtTable( $h, $d, $l = NULL, $w = REPORT_WIDTH ) {
75 
76  $t = NULL;
77 
78  if( empty( $l ) ) { $l = array_keys( $h ); }
79 
80  array_unshift( $d, array_combine( array_keys( $h ), $l ) );
81 
82  foreach( $d as $n => $r ) {
83  foreach( $h as $k => $v ) {
84  if( substr( $k, 0, 9 ) == 'timestamp' && is_numeric( trim( $r[ $k ] ) ) && ! empty( $r[ $k ] ) ) { $r[ $k ] = date( 'Y-m-d H:i:s', $r[ $k ] ); }
85  if( empty( $r[ $k ] ) ) { $r[ $k ] = '-'; }
86  echo txt2fixed( $r[ $k ], $v );
87  }
88  echo PHP_EOL;
89  if( $n === array_key_first( $d ) ) { echo txtLine() . PHP_EOL; }
90  }
91 
92  }
93 
94  function txtText( $t, $j = true, $w = REPORT_WIDTH ) {
95 
96  $t = wordwrap( $t, $w, "\n" );
97 
98  if( $j === true ) {
99  $lines = explode( "\n", $t );
100  foreach( $lines as &$line ) {
101  if( substr( $line, -1 ) != '.' ) {
102  $line = justify( $line );
103  }
104  }
105  return implode( "\n", $lines );
106  } else {
107  return $t;
108  }
109 
110  }
111 
112 function justify( $str, $maxlen = REPORT_WIDTH) {
113 
114  $str = trim($str);
115 
116  $strlen = mb_strlen($str);
117 
118  if ($strlen >= $maxlen) {
119  $str = wordwrap($str, $maxlen);
120  $str = explode("\n", $str);
121  $str = $str[0];
122  $strlen = mb_strlen($str);
123  }
124 
125  $space_count = mb_substr_count($str, ' ');
126  if ($space_count === 0) {
127  return str_pad($str, $maxlen, ' ', STR_PAD_BOTH);
128  }
129 
130  $extra_spaces_needed = $maxlen - $strlen;
131  $total_spaces = $extra_spaces_needed + $space_count;
132 
133  $space_string_avg_length = $total_spaces / $space_count;
134  $short_string_multiplier = floor($space_string_avg_length);
135  $long_string_multiplier = ceil($space_string_avg_length);
136 
137  $short_fill_string = str_repeat(' ', $short_string_multiplier);
138  $long_fill_string = str_repeat(' ', $long_string_multiplier);
139 
140  $offset = $space_string_avg_length - $short_string_multiplier;
141  $limit = ceil( $offset * $space_count );
142  $explode_limit = $limit + 1;
143 # echo gettype( $limit ) . PHP_EOL;
144 # var_dump( $limit );
145 
146  // $words_split_by_long = mb_split("\s", $str, ($limit+1));
147  $words_split_by_long = explode(' ', $str, $explode_limit );
148 # $words_split_by_long = explode(' ', $str, 3);
149  $words_split_by_short = $words_split_by_long[$limit];
150  $words_split_by_short = str_replace(' ', $short_fill_string, $words_split_by_short);
151  $words_split_by_long[$limit] = $words_split_by_short;
152 
153 # print_r( $words_split_by_long );
154 
155  $result = implode($long_fill_string, $words_split_by_long);
156 /*
157  echo '--> ' . $str . PHP_EOL;
158  echo 'stringa: ' . $strlen . PHP_EOL;
159  echo 'spazi: ' . $space_count . PHP_EOL;
160  echo 'spazi aggiuntivi richiesti: ' . $extra_spaces_needed . PHP_EOL;
161  echo 'spaziatura media: ' . $space_string_avg_length . PHP_EOL;
162  echo 'max: ' . $maxlen . PHP_EOL;
163  echo 'fill piccolo: ' . $short_string_multiplier . PHP_EOL;
164  echo 'fill grande: ' . $long_string_multiplier . PHP_EOL;
165  echo 'fill grandi richiesti: ' . $limit . PHP_EOL;
166  echo 'limite di explode: ' . $explode_limit . PHP_EOL;
167  echo 'offset: ' . $offset . PHP_EOL;
168  echo 'limite calcolato con: (' . $space_string_avg_length . ' - ' . $short_string_multiplier . ') * ' . $space_count . PHP_EOL;
169  echo 'segmenti per il fill lungo: ' . implode( '|', $words_split_by_long ) . PHP_EOL;
170  echo 'segmenti per il fill corto: ' . $words_split_by_short . PHP_EOL;
171  echo PHP_EOL;
172 */
173  return $result;
174 }
175 
176  function txt2fixed( $t, $w ) {
177 
178  if( strlen( $t ) >= $w ) {
179  $x = riduciStringa( $t, $w );
180  } else {
181  $x = str_pad( $t, $w - 1 );
182  }
183 
184  return $x . ' ';
185 
186  }
187 
188 ?>
txtFullLine( $c='-', $w=REPORT_WIDTH)
Definition: _txt.tools.php:42
txtLine( $c='-', $w=REPORT_WIDTH)
Definition: _txt.tools.php:36
txtData( $l, $d, $c=' ', $w=REPORT_WIDTH)
Definition: _txt.tools.php:58
$f
Definition: _filesystem.php:21
if(!empty($_REQUEST['id'])) $d
const REPORT_WIDTH
Definition: _txt.tools.php:13
txtText( $t, $j=true, $w=REPORT_WIDTH)
Definition: _txt.tools.php:94
txtHeader( $t, $c='=', $w=REPORT_WIDTH)
Definition: _txt.tools.php:20
riduciStringa( $s, $l, $c='~', $t=CUT_CENTER)
$r
Definition: _osm.php:25
justify( $str, $maxlen=REPORT_WIDTH)
Definition: _txt.tools.php:112
txtDateTime( $f='Y-m-d H:i:s', $d=NULL)
Definition: _txt.tools.php:30
txtTable( $h, $d, $l=NULL, $w=REPORT_WIDTH)
Definition: _txt.tools.php:74
txtDateTimeLine( $c='-', $d=NULL, $w=REPORT_WIDTH)
Definition: _txt.tools.php:48
txt2fixed( $t, $w)
Definition: _txt.tools.php:176