3

任意の配列をテキスト テーブルに変換するこのコードがあります。バグが見つかった場合は、ほぼ完全に機能します。

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);

戻り値:

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

4 に答える 4

4

私は自分の質問を自己解決しました。新しいコードは次のとおりです。

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");
}
于 2010-12-22T04:36:06.813 に答える
2

これはあなたが探しているものでなければなりません。GET パラメータを行として ArrayToTextTable 関数に渡す関数を作成する必要がありますが、機能します。

于 2010-12-22T01:33:13.970 に答える
0

以下にリンクされている URL に、まさにそれを行うためのコードがいくつかあります。

出力例:

│        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.             │
└──────────────────┴──────────────────┴──────────────────┴──────────────────┘

表の幅(列数)を調整できます。

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

于 2014-05-21T06:22:33.000 に答える