简体   繁体   English

如何使用 PHP 在 CSV 中放置 HTML 标签?

[英]How to place HTML tags in a CSV using PHP?

I have an excel sheet.我有一张 excel 表。 In this excel sheet it contains Parts Number , Summary , Brand , Status and Country .在此 excel 表中,它包含Parts NumberSummaryBrandStatusCountry I need to reprocess this csv to create another csv.我需要重新处理这个 csv 以创建另一个 csv。 I would like to be able to import CSV data which contains text that already has html so I don't have to apply all the formatting after import.我希望能够导入 CSV 数据,其中包含已经具有 html 的文本,因此我不必在导入后应用所有格式。

In this new csv, it should only contain 3 columns, Parts Number , Summary and html table .在这个新的 csv 中,它应该只包含 3 列,部件号摘要html 表 In this html table , it contains Parts Number , Summary , Brand , Status and Country .在这个html 表中,它包含Parts NumberSummaryBrandStatusCountry Parts Number and Summary should be in plain text whereas the third column should be in a html table.部件号摘要应为纯文本,而第三列应在 html 表中。

I have already created a table using php and it worked, now I am having difficulty trying to bring the table to csv.我已经使用 php 创建了一个表并且它有效,现在我很难尝试将表带到 csv。 The end result in the csv should look like this, csv 的最终结果应如下所示,

12345 , summary text , <div class=""></div> 12345summary text<div class=""></div>

Here is my code:这是我的代码:

<style type="text/css">
    table {
        border-collapse: collapse;
        border-color: #a9dce4;
        width: 100%;
        position: relative;
        top: -16px;
        border-bottom: 1px solid #8BB4BC;
    }
    tr {
        border-bottom: 1px dotted #a9dce4;
    }
    td {
        padding: 9px;
    }
    th {
        padding: 9px;
    }
</style>   

<?php
$file = fopen("Book1.csv","r");
$file2 = fopen("Book2.csv","w");
$data = [];
$description = '<table class="table">';
while($row = fgetcsv($file)) {
$data[] = $row; //Get all the data
}
if($data){  
    $n_columns = count($data[0]); //Get number of columns 
}
$description .= '<table border="1">'; 
for ($col = 0; $col < $n_columns; $col++) {
    $description .= '<tr>';
    foreach ($data as $row_k => $row) {
        if ($row_k == 0) {
            $description .= '<th>';
        } else {
            $description .= '<td>';
        }

        $description .= $row[$col] ?? '';
        if ($row_k == 0) {
            $description .= '</th>';
        } else {
            $description .= '</td>';
        }
    }
    $description .= '</tr>';
    fputcsv($file2, $data); // line 50
}
$description .= '</table>';
fclose($file);
fclose($file2);
?>

The error message I receive is:我收到的错误信息是:

Notice: Array to string conversion in C:\Users\Marketing\Desktop\xampp\htdocs\csvtable.php on line 50

Here is how Book1.csv looks like:这是 Book1.csv 的样子: 在此处输入图像描述

I am desperate, any help is appreciated.我很绝望,任何帮助表示赞赏。

If I understood you correctly, this should accomplish the task.如果我理解正确,这应该可以完成任务。 It uses 3 functions, array_to_csv , csv_to_array and build_table .它使用 3 个函数, array_to_csvcsv_to_arraybuild_table

<?php

function csv_to_array($filename, $header_row=true)
{
    $handle = fopen($filename, 'r');
        
    if ($header_row === true)
        $header = fgetcsv($handle);

    $data = [];
    while ($row = fgetcsv($handle)) {
        if ($header_row)
            $data[] = array_combine($header, $row);
        else
            $data[] = $row;
    }
    return $data;
}

function array_to_csv($data, $filename, $header_row=true)
{
    $handle = fopen($filename, 'w');

    if ($header_row == true) {
        $header = array_keys($data[0]);
        fputcsv($handle, $header);
    }

    foreach ($data as $line) {
        fputcsv($handle, $line);
    }
    fclose($handle);
}

function build_table($array)
{
    $table = '<table><thead><tr>';
    $headings = array_keys($array[0]);
    foreach ($headings as $heading)
        $table .= '<th>' . $heading . '</th>';    
    $table .= '</tr></thead>';

    $table .= '<tbody>';
    foreach ($array as $row) {
        $table .= '<tr>';
        foreach ($row as $element) {
            $table .= '<td>' . $element . '</td>';
        }
        $table .= '</tr>';
    }
    $table .= '</tbody>';
    return $table;
}

$book1 = csv_to_array('Book1.csv');
$book2 = [];
foreach ($book1 as $row) {
    $key = $row['Parts Number'];
    if (!isset($book2[$key])) {
        $book2[$key] = [
            'Parts Number' => $key,
            'Summary' => $row['Summary']
        ];
    }
    $book2[$key]['rows'][] = $row;
}

foreach ($book2 as &$product) {
    $product['html table'] = build_table($product['rows']);
    unset($product['rows']);
}
unset($product);
$book2 = array_values($book2);

array_to_csv($book2, 'Book2.csv');

Book1.csv contains: Book1.csv 包含:

Parts Number,Summary,Brand,Status,Country
6SE1200-1EA70-1,SIMOVERT P 6,Siemens,Discontinued,Germany
6SE1200-1EA70-1,SIMOVERT P 6,Siemens,Discontinued,France
6SE1200-1EA70-1,SIMOVERT P 6,Siemens,In Stock,England
6SE3012-0BA00,SIMOVERT P M,Siemens,Discontinued,-

Book2.csv output: Book2.csv output:

"Parts Number",Summary,"html table"
6SE1200-1EA70-1,"SIMOVERT P 6","<table><thead><tr><th>Parts Number</th><th>Summary</th><th>Brand</th><th>Status</th><th>Country</th></tr></thead><tbody><tr><td>6SE1200-1EA70-1</td><td>SIMOVERT P 6</td><td>Siemens</td><td>Discontinued</td><td>Germany</td></tr><tr><td>6SE1200-1EA70-1</td><td>SIMOVERT P 6</td><td>Siemens</td><td>Discontinued</td><td>France</td></tr><tr><td>6SE1200-1EA70-1</td><td>SIMOVERT P 6</td><td>Siemens</td><td>In Stock</td><td>England</td></tr></tbody></table>"
6SE3012-0BA00,"SIMOVERT P M","<table><thead><tr><th>Parts Number</th><th>Summary</th><th>Brand</th><th>Status</th><th>Country</th></tr></thead><tbody><tr><td>6SE3012-0BA00</td><td>SIMOVERT P M</td><td>Siemens</td><td>Discontinued</td><td>-</td></tr></tbody></table>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM