JamesM-SiteGen JamesM-SiteGen - 5 days ago 5
PHP Question

Turn any array into a Text Table

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/ |
+-------------------+-------+---------------------------+

Answer

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");
}
Comments