PHP如何使我的csv表正确显示?

时间:2018-11-21 07:00:33

标签: php csv

嗨,我有一个问题,我的代码无法正确读取我的电话号码。我尝试读取的文件是一个用逗号分隔的.csv文件。任何帮助表示赞赏。 我还没有找到任何可以满足我需要的东西,如果我将替换为,它会起作用。但对于我的项目,我需要使用它。

<?php
// inkludere vores footer hvis logget ind
if ($user->is_loggedin() == true) {
    // File selector
    $path = "./assets/csv"; 

    $latest_ctime = 0;
    $latest_filename = '';    

    $d = dir($path);
    while (false !== ($entry = $d->read())) {
    $filepath = "{$path}/{$entry}";
    // could do also other checks than just checking whether the entry is a file
        if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
            $latest_ctime = filectime($filepath);
            $latest_filename = $entry;
        }
    }

    // table start
    echo'<h1 class="text-center">CSV Table</h1>';
    echo'<h6 class="text-center">'.$latest_filename.'</h6>';
    echo '<table id="example" class=" table table-striped table-bordered" style="width:100%">';
    echo'<tbody>';

    $f = fopen("$path/$latest_filename", "r");

    while (($line = fgetcsv($f)) !== false) {

            $row = $line[0];    // We need to get the actual row (it is the first element in a 1-element array)

            $cells = explode(";",$row);
            echo '<tr>';
            foreach ($cells as $cell) {
                echo '<td>' .htmlspecialchars($cell, ENT_COMPAT). '</td>';

            }
            echo '</tr>';
    }
    fclose($f);
    echo'</tbody>';
    echo '</table>';
}

表格在excel中看起来像

https://gyazo.com/49e8b16d369b1fa61496601952ca953b

这是它在我的网站上的样子

https://gyazo.com/b271c3a221587b2e0c6b7ee4cebd6bdb 根据我的阅读,出现这些错误的原因是因为我需要UTF-8才能阅读我的语言

1 个答案:

答案 0 :(得分:0)

我终于找到了答案...经过长时间的测试,我终于找到了答案

//文件选择器         $ path =“ ./assets/csv”;

    $latest_ctime = 0;
    $latest_filename = '';    

    $d = dir($path);
    while (false !== ($entry = $d->read())) {
    $filepath = "{$path}/{$entry}";
    // could do also other checks than just checking whether the entry is a file
        if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
            $latest_ctime = filectime($filepath);
            $latest_filename = $entry;
        }
    }


    // table start
    echo'<h1 class="text-center">CSV Table</h1>';
    echo'<h6 class="text-center">'.$latest_filename.'</h6>';
    echo '<table id="example" class=" table table-striped table-bordered" style="width:100%">';
    echo'<tbody>';

    $f = fopen("$path/$latest_filename", "r");

    while (($line = fgetcsv($f, 1000, ";")) !== false) {

            $row = $line[0];    // We need to get the actual row (it is the first element in a 1-element array)

            // $cells = explode(";",$row);
            echo '<tr>';
            foreach ($line as $cell) {
                echo '<td>' .htmlspecialchars($cell, ENT_COMPAT). '</td>';

            }
            echo '</tr>';
    }
    fclose($f);
    echo'</tbody>';
    echo '</table>';
 }

希望有人可以使用它,我注意到此解决方案是,我认为while (($line = fgetcsv($f, 1000, ";")) !== false) {。香港专业教育学院测试它,它可能会超过10000行。无论如何,我发现节省了一些时间。我肯定可以用它。