├── ascii_table.php ├── composer.json ├── example.txt ├── examples.php └── readme.md /ascii_table.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2018 Phillip Gooch 10 | * @link https://github.com/pgooch/PHP-Ascii-Tables 11 | */ 12 | class Ascii_Table 13 | { 14 | /** 15 | * These are all the variables that the script uses to build out the table, none of them meant for user-modification. 16 | */ 17 | 18 | /** 19 | * An array that contains the max character width of each column (not including buffer spacing). 20 | * @access private 21 | */ 22 | private $col_widths = array(); 23 | 24 | /** 25 | * An array that contains the column types. 26 | * @access private 27 | */ 28 | private $col_types = array(); 29 | 30 | /** 31 | * The complete width of the table, including spacing and bars. 32 | * @access private 33 | */ 34 | private $table_width = 0; 35 | 36 | /** 37 | * The error reported by file_put_contents or file_get_contents when it fails a save or load attempt. 38 | * @access public 39 | */ 40 | public $error = ''; 41 | 42 | /** 43 | * This is the function that you will call to make the table. You must pass it at least the first variable. 44 | * 45 | * @param array $array A multi-dimensional array containing the data you want to build a table from. 46 | * @param string $title The title of the table that will be centered above it, if you do not want a title you can pass a blank. 47 | * @param boolean $return The method of returning the data, this has 3 options. 48 | * True - The script will return the table as a string. (Required). 49 | * False - The script will echo the table out, nothing will be returned. 50 | * String - It will attempt to save the table to a file with the strings name, Returning true/false of success or fail. 51 | * @param boolean $autoalign_cells If True all column names and values with numeric data types will be aligned to the right of the cell. 52 | * 53 | * @return string|bool|void The ASCII table representation of $array. 54 | */ 55 | public function make_Table($array, $title = '', $return = false, $autoalign_cells = false) 56 | { 57 | // First things first lets get the variable ready 58 | $table = ''; 59 | $this->col_widths = array(); 60 | $this->col_types = array(); 61 | $this->table_width = 0; 62 | 63 | // Modify the table to support any line breaks that might exist 64 | $modified_array = array(); 65 | foreach ($array as $row => $row_data) { 66 | // This will break the cells up on line breaks and store them in $raw_array with the longest value for that column in $longest_cell 67 | $row_array = array(); 68 | $longest_cell = 1; 69 | foreach ($row_data as $cell => $cell_value) { 70 | $cell_value = explode("\n", $cell_value); 71 | $row_array[$cell] = $cell_value; 72 | $longest_cell = max($longest_cell, count($cell_value)); 73 | } 74 | 75 | // This will loop as many times as the longest, if there is a value it will use that, if not it will just give it an empty string 76 | for ($i = 0; $i < $longest_cell; $i++) { 77 | $new_row_temp = array(); 78 | foreach ($row_array as $col => $col_data) { 79 | if (isset($col_data[$i])) { 80 | $new_row_temp[$col] = trim($col_data[$i]); 81 | } else { 82 | $new_row_temp[$col] = ''; 83 | } 84 | } 85 | $modified_array[] = $new_row_temp; 86 | } 87 | } 88 | 89 | // Finally we can call the fully modified array the array for future use 90 | $array = $modified_array; 91 | 92 | // Now we need to get some details prepared. 93 | $this->getColWidths($array); 94 | $this->getColTypes($array); 95 | 96 | // If there is going to be a title we are also going to need to determine the total width of the table, otherwise we don't need it 97 | if ($title != '') { 98 | $this->getTableWidth(); 99 | $table .= $this->makeTitle($title); 100 | } 101 | 102 | // If we have a blank array then we don't need to output anything else 103 | if (isset($array[0])) { 104 | // Now we can output the header row, along with the divider rows around it 105 | $table .= $this->makeDivider(); 106 | 107 | // Output the header row 108 | $table .= $this->makeHeaders($autoalign_cells); 109 | 110 | // Another divider line 111 | $table .= $this->makeDivider(); 112 | 113 | // Add the table data in 114 | $table .= $this->makeRows($array, $autoalign_cells); 115 | 116 | // The final divider line. 117 | $table .= $this->makeDivider(); 118 | } 119 | 120 | // Now handle however you want this returned 121 | // First if it's a string were saving 122 | if (is_string($return)) { 123 | $save = @file_put_contents($return, $table); 124 | if ($save) { 125 | 126 | return true; 127 | } else { 128 | // Add the save_error if there was one 129 | $this->error = 'Unable to save table to "' . $return . '".'; 130 | 131 | return false; 132 | } 133 | } else { 134 | // the bool returns are very simple. 135 | if ($return) { 136 | 137 | return $table; 138 | } else { 139 | echo $table; 140 | } 141 | } 142 | } 143 | 144 | /** 145 | * This function will load a saved ascii table and turn it back into a multi-dimensional table. 146 | * 147 | * @param string $table A PHP ASCII Table either as a string or a text file. 148 | * 149 | * @return array Return a multi-dimensional array similar to the one that you would have given it `make_table()` to create it. 150 | */ 151 | public function break_Table($table) 152 | { 153 | // Try and load the file, if it fails then just return false and set an error message 154 | $load_file = @file_get_contents($table); 155 | if ($load_file !== false) { 156 | $table = $load_file; 157 | } 158 | 159 | // First thing we want to do is break it apart at the lines 160 | $table = explode(PHP_EOL, trim($table)); 161 | 162 | // Check if the very first character of the very first row is a +, if not delete that row, it must be a title. 163 | if (substr($table[0], 0, 1) != '+') { 164 | unset($table[0]); 165 | $table = array_values($table); 166 | } 167 | 168 | // Were going to need a few variables ready-to-go, so lets do that 169 | $array = array(); 170 | $array_columns = array(); 171 | 172 | // Now we want to grab row [1] and get the column names from it. 173 | $columns = explode('|', $table[1]); 174 | 175 | foreach ($columns as $n => $value) { 176 | // The first and last columns are blank, so lets skip them 177 | if ($n > 0 && $n < count($columns) - 1) { 178 | // If we have a value after trimming the whitespace then use it, otherwise just give the column it's number as it's name 179 | if (trim($value) != '') { 180 | $array_columns[$n] = trim($value); 181 | } else { 182 | $array_columns[$n] = $n; 183 | } 184 | } 185 | } 186 | 187 | // And now we can go through the bulk of the table data 188 | for ($row = 3; $row < count($table) - 1; $row++) { 189 | // Break the row apart on the pipe as well 190 | $row_items = explode('|', $table[$row]); 191 | 192 | // Loop through all the array columns and grab the appropriate value, placing it all in the $array variable. 193 | foreach ($array_columns as $pos => $column) { 194 | // Add the details into the main $array table, remembering to trim them of that extra whitespace 195 | $array[$row][$column] = trim($row_items[$pos]); 196 | } 197 | } 198 | 199 | // Reflow the array so that it starts at the logical 0 point 200 | $array = array_values($array); 201 | 202 | // Return the array 203 | return $array; 204 | } 205 | 206 | /** 207 | * This will take a table in either a file or a string and scrape out two columns of data from it. If you only pass a single column it will return that in a straight numeric array. 208 | * 209 | * @param string $table The table file or string. 210 | * @param string $key They column to be used as the array key, if no value is passed, the value that will be placed in the numeric array. 211 | * @param string $value The column to be used as the array value, if null then key will be returned in numeric array. 212 | * 213 | * @return array Return they key/value pairs requested. 214 | */ 215 | public function scrape_Table($table, $key, $value = null) 216 | { 217 | // First things first wets parse the entire table out. 218 | $table = $this->break_Table($table); 219 | 220 | // Set up a variable to store the return in while processing 221 | $array = array(); 222 | 223 | // Now we loop through the table 224 | foreach ($table as $row => $data) { 225 | // If value is null then set it to key and key to row. 226 | if ($value == null) { 227 | $grabbed_value = $data[$key]; 228 | $grabbed_key = $row; 229 | // Else just grab the desired key/value values 230 | } else { 231 | $grabbed_key = $data[$key]; 232 | $grabbed_value = $data[$value]; 233 | } 234 | 235 | // Place the information into the array(). 236 | $array[$grabbed_key] = $grabbed_value; 237 | } 238 | 239 | // Finally return the new array 240 | return $array; 241 | } 242 | 243 | /** 244 | * This function will use the mb_strlen if available or strlen. 245 | * 246 | * @param string $col_value The string that be need to be counted. 247 | * 248 | * @return int Returns a lenght of string using mb_strlen or strlen. 249 | */ 250 | private static function len($col_value) 251 | { 252 | return extension_loaded('mbstring') ? mb_strlen($col_value) : strlen($col_value); 253 | } 254 | 255 | /** 256 | * This method will set the $col_width variable with the longest value in each column. 257 | * 258 | * @param array $array The multi-dimensional array you are building the ASCII Table from. 259 | * 260 | * @return void 261 | */ 262 | private function getColWidths($array) 263 | { 264 | // If we have some array data loop through each row, then through each cell 265 | if (isset($array[0])) { 266 | foreach (array_keys($array[0]) as $col) { 267 | // Get the longest col value and compare with the col name to get the longest 268 | $this->col_widths[$col] = max(max(array_map(array($this, 'len'), $this->arrCol($array, $col))), $this->len($col)); 269 | } 270 | } 271 | } 272 | 273 | /** 274 | * This method will set the $col_types variable with the type of value in each column. 275 | * 276 | * @param array $array The multi-dimensional array you are building the ASCII Table from. 277 | * 278 | * @return void 279 | */ 280 | private function getColTypes($array) 281 | { 282 | // If we have some array data loop through each row, then through each cell 283 | if (isset($array[0])) { 284 | // Parse each col and each row to get the column type 285 | foreach (array_keys($array[0]) as $col) { 286 | foreach ($array as $i => $row) { 287 | if (trim($row[$col]) != '') { 288 | if (!isset($this->col_types[$col])) { 289 | $this->col_types[$col] = is_numeric($row[$col]) ? 'numeric' : 'string'; 290 | } else { 291 | if ($this->col_types[$col] == 'numeric') { 292 | $this->col_types[$col] = is_numeric($row[$col]) ? 'numeric' : 'string'; 293 | } 294 | } 295 | } 296 | } 297 | } 298 | } 299 | } 300 | 301 | /** 302 | * This is an array_column shim, it will use the PHP array_column function if there is one, otherwise it will do the same thing the old way. 303 | * 304 | * @param array $array The multi-dimensional array you are building the ASCII Table from. 305 | * @param string $col A table's key (column). 306 | * 307 | * @return array An array containing all values of a column. 308 | */ 309 | private function arrCol($array, $col) 310 | { 311 | if (is_callable('array_column')) { 312 | $return = array_column($array, $col); 313 | } else { 314 | $return = array(); 315 | foreach ($array as $n => $dat) { 316 | if (isset($dat[$col])) { 317 | $return[] = $dat[$col]; 318 | } 319 | } 320 | } 321 | 322 | return $return; 323 | } 324 | 325 | /** 326 | * This will get the entire width of the table and set $table_width accordingly. This value is used when building. 327 | * 328 | * @return void 329 | */ 330 | private function getTableWidth() 331 | { 332 | // Add up all the columns 333 | $this->table_width = array_sum($this->col_widths); 334 | 335 | // Add in the spacers between the columns (one on each side of the value) 336 | $this->table_width += count($this->col_widths) * 2; 337 | 338 | // Add in the dividers between columns, as well as the ones for the outside of the table 339 | $this->table_width += count($this->col_widths) + 1; 340 | } 341 | 342 | /** 343 | * This will return the centered title (only called if a title is passed). 344 | * 345 | * @param string $title The table's title. 346 | * 347 | * @return string The centered title. 348 | */ 349 | private function makeTitle($title) 350 | { 351 | // First we want to remove any extra whitespace for a proper centering 352 | $title = trim($title); 353 | 354 | // Determine the padding needed on the left side of the title 355 | $left_padding = floor(($this->table_width - $this->len($title)) / 2); 356 | 357 | // return exactly what is needed 358 | return str_repeat(' ', max($left_padding, 0)) . $title . PHP_EOL; 359 | } 360 | 361 | /** 362 | * This will use the data in the $col_width var to make a divider line. 363 | * 364 | * @return string A table's divider. 365 | */ 366 | private function makeDivider() 367 | { 368 | // were going to start with a simple union piece 369 | $divider = '+'; 370 | 371 | // Loop through the table, adding lines of the appropriate length (remembering the +2 for the spacers), and a union piece at the end 372 | foreach ($this->col_widths as $col => $length) { 373 | $divider .= str_repeat('-', $length + 2) . '+'; 374 | } 375 | 376 | // return it 377 | return $divider . PHP_EOL; 378 | } 379 | 380 | /** 381 | * This will look through the $col_widths array and make a column header for each one. 382 | * 383 | * @param bool $autoalign_cells If True, columns with numeric data types will be aligned to the right of the cell. 384 | * 385 | * @return string The row of the table header. 386 | */ 387 | private function makeHeaders($autoalign_cells) 388 | { 389 | // This time were going to start with a simple bar; 390 | $row = '|'; 391 | 392 | // Loop though the col widths, adding the cleaned title and needed padding 393 | foreach ($this->col_widths as $col => $length) { 394 | // Add title 395 | $alignment = $autoalign_cells && isset($this->col_types[$col]) && $this->col_types[$col] == 'numeric' ? STR_PAD_LEFT : STR_PAD_RIGHT; 396 | $row .= ' ' . str_pad($col, $this->col_widths[$col], ' ', $alignment) . ' '; 397 | 398 | // Add the right hand bar 399 | $row .= '|'; 400 | } 401 | 402 | // Return the row 403 | return $row . PHP_EOL; 404 | } 405 | 406 | /** 407 | * This makes the actual table rows. 408 | * 409 | * @param array $array The multi-dimensional array you are building the ASCII Table from. 410 | * @param bool $autoalign_cells If True, column values with numeric data types will be aligned to the right of the cell. 411 | * 412 | * @return string The rows of the table. 413 | */ 414 | private function makeRows($array, $autoalign_cells) 415 | { 416 | // Just prep the variable 417 | $rows = ''; 418 | 419 | // Loop through rows 420 | foreach ($array as $n => $data) { 421 | // Again were going to start with a simple bar 422 | $rows .= '|'; 423 | 424 | // Loop through the columns 425 | foreach ($data as $col => $value) { 426 | // Add the value to the table 427 | $alignment = $autoalign_cells && isset($this->col_types[$col]) && $this->col_types[$col] == 'numeric' ? STR_PAD_LEFT : STR_PAD_RIGHT; 428 | $rows .= ' ' . str_pad($value, $this->col_widths[$col], ' ', $alignment) . ' '; 429 | 430 | // Add the right hand bar 431 | $rows .= '|'; 432 | } 433 | 434 | // Add the row divider 435 | $rows .= PHP_EOL; 436 | } 437 | 438 | // Return the row 439 | return $rows; 440 | } 441 | } 442 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pgooch/php-ascii-tables", 3 | "version": "1.1.0", 4 | "type": "library", 5 | "description": "Convert multi-dimensional arrays into ASCII Tabled, and vise-versa.", 6 | "keywords": ["cli","ascii","tables"], 7 | "homepage": "https://github.com/pgooch/PHP-Ascii-Tables", 8 | "license": "GPLv3", 9 | "authors": [ 10 | { 11 | "name": "Phillip Gooch", 12 | "email": "phillip.gooch@gmail.com", 13 | "homepage": "http://phillipgooch.com", 14 | "role": "Developer" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=5.0.0" 19 | } 20 | } -------------------------------------------------------------------------------- /example.txt: -------------------------------------------------------------------------------- 1 | Seattle Zip Codes 2 | +----------+-------+---------+------------+---------------+---------------+ 3 | | Zip Code | Lat | Lon | Population | Housing Units | Median Income | 4 | +----------+-------+---------+------------+---------------+---------------+ 5 | | 98101 | 47.61 | -122.33 | 9,010 | 6,673 | 58,713 | 6 | | 98102 | 47.63 | -122.32 | 19,424 | 12,913 | 68,969 | 7 | | 98103 | 47.67 | -122.34 | 41,971 | 21,886 | 66,949 | 8 | | 98104 | 47.6 | -122.32 | 13,095 | 5,928 | 24,601 | 9 | | 98105 | 47.66 | -122.3 | 38,963 | 15,317 | 71,754 | 10 | | 98106 | 47.53 | -122.35 | 23,317 | 8,833 | 44,115 | 11 | | 98107 | 47.66 | -122.37 | 18,516 | 10,151 | 59,688 | 12 | | 98108 | 47.54 | -122.31 | 21,223 | 7,583 | 43,942 | 13 | | 98109 | 47.63 | -122.34 | 16,018 | 10,100 | 76,307 | 14 | | 98110 | 47.65 | -122.53 | 20,308 | 8,517 | 83,415 | 15 | | 98111 | 47.6 | -122.33 | | | | 16 | | 98112 | 47.62 | -122.29 | 20,480 | 10,414 | 98,709 | 17 | | 98113 | 47.6 | -122.33 | | | | 18 | | 98114 | 47.6 | -122.32 | | | | 19 | | 98115 | 47.68 | -122.3 | 43,567 | 20,185 | 76,422 | 20 | | 98116 | 47.57 | -122.39 | 20,826 | 10,990 | 75,381 | 21 | | 98117 | 47.68 | -122.37 | 29,667 | 13,624 | 67,342 | 22 | | 98118 | 47.54 | -122.27 | 40,791 | 14,574 | 49,849 | 23 | | 98119 | 47.63 | -122.36 | 19,662 | 10,836 | 79,177 | 24 | | 98121 | 47.61 | -122.34 | 8,558 | 6,741 | 66,700 | 25 | | 98122 | 47.61 | -122.3 | 28,790 | 14,588 | 43,984 | 26 | | 98124 | 47.6 | -122.33 | | | | 27 | | 98125 | 47.71 | -122.3 | 34,994 | 16,446 | 56,628 | 28 | | 98126 | 47.54 | -122.37 | 18,906 | 8,701 | 57,193 | 29 | | 98127 | 47.6 | -122.33 | | | | 30 | | 98129 | 47.6 | -122.33 | | | | 31 | | 98131 | 47.6 | -122.33 | | | | 32 | | 98132 | 47.6 | -122.33 | | | | 33 | | 98133 | 47.73 | -122.34 | 42,896 | 19,734 | 54,093 | 34 | | 98134 | 47.57 | -122.33 | 636 | 46 | | 35 | | 98136 | 47.53 | -122.38 | 14,138 | 7,020 | 73,750 | 36 | | 98138 | 47.45 | -122.25 | | | | 37 | | 98139 | 47.6 | -122.33 | | | | 38 | | 98141 | 47.6 | -122.33 | | | | 39 | | 98144 | 47.58 | -122.3 | 24,913 | 10,420 | 48,217 | 40 | | 98145 | 47.65 | -122.31 | | | | 41 | | 98146 | 47.5 | -122.35 | 25,574 | 10,022 | 52,691 | 42 | | 98148 | 47.44 | -122.33 | 9,519 | 4,552 | 46,607 | 43 | | 98151 | 47.6 | -122.33 | | | | 44 | | 98154 | 47.6 | -122.33 | 1 | | | 45 | | 98155 | 47.75 | -122.3 | 33,914 | 13,463 | 66,172 | 46 | | 98158 | 47.44 | -122.3 | | | | 47 | | 98160 | 47.76 | -122.36 | | | | 48 | | 98161 | 47.6 | -122.33 | | | | 49 | | 98164 | 47.6 | -122.33 | 1 | | | 50 | | 98165 | 47.6 | -122.33 | | | | 51 | | 98166 | 47.45 | -122.34 | 20,302 | 8,808 | 65,385 | 52 | | 98168 | 47.49 | -122.3 | 30,198 | 11,908 | 46,099 | 53 | | 98170 | 47.6 | -122.33 | | | | 54 | | 98171 | 47.6 | -122.33 | | | | 55 | | 98174 | 47.6 | -122.33 | 180 | | | 56 | | 98175 | 47.6 | -122.33 | | | | 57 | | 98177 | 47.74 | -122.36 | 18,920 | 7,697 | 77,490 | 58 | | 98178 | 47.49 | -122.24 | 21,860 | 8,725 | 54,198 | 59 | | 98181 | 47.6 | -122.33 | | | | 60 | | 98184 | 47.6 | -122.33 | | | | 61 | | 98185 | 47.66 | -122.29 | | | | 62 | | 98188 | 47.45 | -122.27 | 22,583 | 10,118 | 46,675 | 63 | | 98190 | 47.6 | -122.33 | | | | 64 | | 98191 | 47.6 | -122.33 | | | | 65 | | 98194 | 47.6 | -122.33 | | | | 66 | | 98195 | 47.65 | -122.31 | | | | 67 | | 98198 | 47.39 | -122.31 | 33,561 | 13,169 | 54,401 | 68 | | 98199 | 47.64 | -122.39 | 19,156 | 9,416 | 79,842 | 69 | +----------+-------+---------+------------+---------------+---------------+ -------------------------------------------------------------------------------- /examples.php: -------------------------------------------------------------------------------- 1 | 'Red', 8 | 'HEX' => '#FF0000', 9 | 'Red' => '255', 10 | 'Green' => '0', 11 | 'Blue' => '0', 12 | 'Hue' => '0', 13 | 'Saturation' => '100', 14 | 'Lightness' => '50', 15 | 'Cyan' => '0', 16 | 'Magenta' => '100', 17 | 'Yellow' => '100', 18 | 'Key' => '0', 19 | ), 20 | array( 21 | 'color' => 'Orange', 22 | 'HEX' => '#FFA500', 23 | 'Red' => '255', 24 | 'Green' => '165', 25 | 'Blue' => '0', 26 | 'Hue' => '39', 27 | 'Saturation' => '100', 28 | 'Lightness' => '50', 29 | 'Cyan' => '0', 30 | 'Magenta' => '100', 31 | 'Yellow' => '35', 32 | 'Key' => '0', 33 | ), 34 | array( 35 | 'color' => 'Yellow', 36 | 'HEX' => '#FFFF00', 37 | 'Red' => '255', 38 | 'Green' => '255', 39 | 'Blue' => '0', 40 | 'Hue' => '60', 41 | 'Saturation' => '100', 42 | 'Lightness' => '50', 43 | 'Cyan' => '0', 44 | 'Magenta' => '0', 45 | 'Yellow' => '100', 46 | 'Key' => '0', 47 | ), 48 | array( 49 | 'color' => 'Green', 50 | 'HEX' => '#008000', 51 | 'Red' => '0', 52 | 'Green' => '128', 53 | 'Blue' => '0', 54 | 'Hue' => '120', 55 | 'Saturation' => '100', 56 | 'Lightness' => '25', 57 | 'Cyan' => '100', 58 | 'Magenta' => '0', 59 | 'Yellow' => '100', 60 | 'Key' => '50', 61 | ), 62 | array( 63 | 'color' => 'Blue', 64 | 'HEX' => '#0000FF', 65 | 'Red' => '0', 66 | 'Green' => '0', 67 | 'Blue' => '255', 68 | 'Hue' => '240', 69 | 'Saturation' => '100', 70 | 'Lightness' => '50', 71 | 'Cyan' => '100', 72 | 'Magenta' => '100', 73 | 'Yellow' => '0', 74 | 'Key' => '0', 75 | ), 76 | array( 77 | 'color' => 'Indigo', 78 | 'HEX' => '#4B0082', 79 | 'Red' => '75', 80 | 'Green' => '0', 81 | 'Blue' => '130', 82 | 'Hue' => '275', 83 | 'Saturation' => '100', 84 | 'Lightness' => '25', 85 | 'Cyan' => '42', 86 | 'Magenta' => '100', 87 | 'Yellow' => '0', 88 | 'Key' => '49', 89 | ), 90 | array( 91 | 'color' => 'Violet', 92 | 'HEX' => '#EE82EE', 93 | 'Red' => '238', 94 | 'Green' => '130', 95 | 'Blue' => '238', 96 | 'Hue' => '300', 97 | 'Saturation' => '76', 98 | 'Lightness' => '72', 99 | 'Cyan' => '0', 100 | 'Magenta' => '45', 101 | 'Yellow' => '0', 102 | 'Key' => '7', 103 | ), 104 | ); 105 | $multiline_test_data = array( 106 | array( 107 | 'id' => 1, 108 | 'multiline' => "This cell \n is exactly \n 3 lines.", 109 | 'ellipse' => '...' 110 | ), 111 | array( 112 | 'id' => 2, 113 | 'multiline' => "This cell is only \n 2 lines", 114 | 'ellipse' => '...' 115 | ), 116 | array( 117 | 'id' => 3, 118 | 'multiline' => "Just a single line", 119 | 'ellipse' => '...' 120 | ), 121 | array( 122 | 'id' => 4, 123 | 'multiline' => "Also a single line", 124 | 'ellipse' => ".\n.\n." 125 | ), 126 | ); 127 | ?> 128 | 129 | 130 | 131 | 132 | PHP ASCII-Table Examples 133 | 134 | 135 |
136 | make_table($multiline_test_data,'Multi-line Cells') ?>
137 | 
138 | make_table($svg_colors,'Colors in Various Formats',true);
140 | echo $table; ?>
141 | 
142 | The color names and hex values, scraped from the "Colors in Various Formats" table above.
143 | scrape_table($table,'color','HEX')); ?>
144 | 
145 | All of the zip codes in Seattle, broken from the example.txt file.
146 | break_table('./example.txt')); ?>
147 | 
148 | What happens when you try and pass an empty array to the make_table function.
149 | make_table(array(),'Blank Test'); ?>
150 |         
151 | 152 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # PHP ASCII-Table 2 | 3 | This class will convert multi-dimensional arrays into ASCII Tables, and vice-versa. 4 | 5 | ### Setup & Use 6 | Include `ascii_table.php` and call a new instance of the object. You can then call one of three functions: 7 | 8 | - `make_Table($array, [$title], [$return], [$autoalign_cells])` will make a table with the multi-dimensional `$array` passed. Additionally you can specify an optional `$title` that will be centered on a line above the table. The `$return` variable gives you 3 options; `True` will return the table as an array, `False` will output the array directly, and a `String` will attempt to save the array in a text file with the given name/location and will return true/false upon completlion. If false the error message will be logged to the `$error` class variable. The parameter `$autoalign_cells`, if set to `True`, all columns containing only numeric datatypes will be aligned to the right of the cell. 9 | - `break_Table($table)` Takes an table or a filename that containing a text file output from `make_table()` and will return a multi-dimensional array similar to the one that you would have given it `make_table()` to create it. 10 | - `scrape_Table($table, $key, [$value])` will take a table or link to a file containing a table as `break_table()` does but will only return they key/value pairs you request. If you do not include the value it will use the key as the value and return it in a numeric array. It should be noted that if you use both key and value that muliple keys will overrite eachother and the returning array will only contain the last one in the table. 11 | 12 | 13 | ### Examples 14 | Examples of the classes functionality can be found in examples.php, and a text output can be found in `example.txt`. Table will be output or returned like this: 15 | 16 | Colors in Various Formats 17 | +--------+---------+-----+-------+------+-----+------------+-----------+------+---------+--------+-----+ 18 | | color | HEX | Red | Green | Blue | Hue | Saturation | Lightness | Cyan | Magenta | Yellow | Key | 19 | +--------+---------+-----+-------+------+-----+------------+-----------+------+---------+--------+-----+ 20 | | Red | #FF0000 | 255 | 0 | 0 | 0 | 100 | 50 | 0 | 100 | 100 | 0 | 21 | | Orange | #FFA500 | 255 | 165 | 0 | 39 | 100 | 50 | 0 | 100 | 35 | 0 | 22 | | Yellow | #FFFF00 | 255 | 255 | 0 | 60 | 100 | 50 | 0 | 0 | 100 | 0 | 23 | | Green | #008000 | 0 | 128 | 0 | 120 | 100 | 25 | 100 | 0 | 100 | 50 | 24 | | Blue | #0000FF | 0 | 0 | 255 | 240 | 100 | 50 | 100 | 100 | 0 | 0 | 25 | | Indigo | #4B0082 | 75 | 0 | 130 | 275 | 100 | 25 | 42 | 100 | 0 | 49 | 26 | | Violet | #EE82EE | 238 | 130 | 238 | 300 | 76 | 72 | 0 | 45 | 0 | 7 | 27 | +--------+---------+-----+-------+------+-----+------------+-----------+------+---------+--------+-----+ 28 | --------------------------------------------------------------------------------