Metin Tablo içine herhangi bir dizi çevirin

0 Cevap php

Ben herhangi bir hata bulursanız lütfen bana, neredeyse mükemmel çalışır, bir metin tabloya herhangi bir dizi döner bu kodu var ..

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

İade:

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

0 Cevap