Question

I have this code that turns any array into a text table, it works almost perfectly, if you find any bugs please tell me..

function arr2textTable($table) {
    function clean($var) { 
        $search=array("`((?:https?|ftp)://\S+[[:alnum:]]/?)`si","`((?<!//)(www\.\S+[[:alnum:]]/?))`si");
        $replace=array("<a href=\"$1\" rel=\"nofollow\">$1</a>","<a href=\"http://$1\" rel=\"nofollow\">$1</a>");
        $var = preg_replace($search, $replace, $var);
        return $var;
    }
    foreach ($table AS $row) {
        $cell_count = 0;
        foreach ($row AS $key=>$cell) {
            $cell_length = strlen($cell);
            $key_length = strlen($key);
            $cell_length = $key_length > $cell_length ? $key_length : $cell_length;
            $cell_count++;
            if (!isset($cell_lengths[$key]) || $cell_length > $cell_lengths[$key])
                $cell_lengths[$key] = $cell_length;
        }   
    }
    $bar = "+";
    $header = "|";
    foreach ($cell_lengths AS $fieldname => $length) {
        $bar .= str_pad("", $length+2, "-")."+";
        $name = $fieldname;
        if (strlen($name) > $length) {
            $name = substr($name, 0, $length-1);
        }
        $header .= " ".str_pad($name, $length, " ", STR_PAD_RIGHT) . " |";
    }
    $output = "${bar}\n${header}\n${bar}\n";
    foreach ($table AS $row) {
        $output .= "|";
        foreach ($row AS $key=>$cell) {
            $output .= " ".str_pad($cell, $cell_lengths[$key], " ", STR_PAD_RIGHT) . " |";
        }
        $output .= "\n";
    }
    $output .= $bar."\n";
    return clean($output);
}

$table=json_decode([{"OS":"Debian","Stars":"10","Link":"http://debian.org/"},{"OS":"Linux Mint Debian","Stars":"9","Link":"http://linuxmint.com/"},{"OS":"Fedora","Stars":"9","Link":"http://fedoraproject.org/"}]);
echo arr2textTable($table);

Returns:

+-------------------+-------+---------------------------+
| OS                | Stars | Link                      |
+-------------------+-------+---------------------------+
| Debian            | 10    | http://debian.org/        |
| Linux Mint Debian | 9     | http://linuxmint.com/     |
| Fedora            | 9     | http://fedoraproject.org/ |
+-------------------+-------+---------------------------+
Was it helpful?

Solution 2

I have self-solved my own question Here is the new code:

function arr2textTable($a, $b = array(), $c = 0) {
    $d = array();
    $e = "+";
    $f = "|";
    $g = 0;
    foreach ($a as $h)
        foreach ($h AS $i => $j) {
            $j = substr(str_replace(array("\n","\r","\t","  "), " ", $j), 0, 48);
            $k = strlen($j);
            $l = strlen($i);
            $k = $l > $k ? $l : $k;
            if (!isset($d[$i]) || $k > $d[$i])
                $d[$i] = $k;
        }
    foreach ($d as $m => $h) {
        $e .= str_pad("", $h + 2, "-") . "+";
            if (strlen($m) > $h)
                $m = substr($m, 0, $h - 1);
            $f .= " " . str_pad($m, $h, " ", isset($b[$g]) ? $b[$g] : $c) . " |";
            $g++;
    }
    $n = "{$e}\n{$f}\n{$e}\n";
    foreach ($a as $h) {
        $n .= "|";
        $g = 0;
        foreach ($h as $i => $o) {
            $n .= " " . str_pad($o, $d[$i], " ", isset($b[$g]) ? $b[$g] : $c) . " |";
            $g++;
        }
        $n .= "\n";
    }
    $p = array(
        "`((?:https?|ftp)://\S+[[:alnum:]]/?)`si",
        "`((?<!//)(www\.\S+[[:alnum:]]/?))`si"
    );
    $q = array(
        "<a href=\"$1\" rel=\"nofollow\">$1</a>",
        "<a href=\"http://$1\" rel=\"nofollow\">$1</a>"
    );
    return preg_replace($p, $q, "{$n}{$e}\n");
}

OTHER TIPS

This should be what you are looking for. You will need to write a function to pass GET params in as rows to the ArrayToTextTable function but it will work.

There is some code to do exactly that at the URL linked below.

Sample output:

│        ID        │       NAME       │     BALANCE      │     COMMENTS     │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ 0                │ Joe              │ 1000000          │ This is the firs │
│                  │                  │                  │ t comment.       │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ 1                │ Jack             │ 500000           │ Lorem ipsum dolo │
│                  │                  │                  │ r sit amet, cons │
│                  │                  │                  │ ectetur adipisci │
│                  │                  │                  │ ng elit. In luct │
│                  │                  │                  │ us pellentesque  │
│                  │                  │                  │ ante, a placerat │
│                  │                  │                  │  purus molestie  │
│                  │                  │                  │ et. Nam urna pur │
│                  │                  │                  │ us, accumsan ege │
│                  │                  │                  │ t dolor consequa │
│                  │                  │                  │ t, ultricies con │
│                  │                  │                  │ vallis nibh.     │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ 2                │ William          │ 10000            │ This_is_a_very_v │
│                  │                  │                  │ ery_very_very_ve │
│                  │                  │                  │ ry_very_very_ver │
│                  │                  │                  │ y_very_very_very │
│                  │                  │                  │ _long_line_with_ │
│                  │                  │                  │ no_spaces_or_lin │
│                  │                  │                  │ e_breaks         │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ 3                │ Avrell           │ 200              │ Avrell is the    │
│                  │                  │                  │ (tall|dumb)est   │
│                  │                  │                  │ one.             │
└──────────────────┴──────────────────┴──────────────────┴──────────────────┘

Table width (number of columns) can be adjusted.

Source: http://opentechnotes.blogspot.com.au/2014/05/display-php-array-as-plaintext-table.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top