Php 如何转换CSV文件';数组中的表值是多少?

Php 如何转换CSV文件';数组中的表值是多少?,php,arrays,csv,html-table,customization,Php,Arrays,Csv,Html Table,Customization,我的CSV文件的第1列中有以下值 <table border="1"> <tr> <th align="left">First Name</th> <th align="left">Gender</th> <th align="left">Nationality</th> </tr> <tr> <td align="left">Mik

我的CSV文件的第1列中有以下值

<table border="1">
<tr>
    <th align="left">First Name</th>
    <th align="left">Gender</th>
    <th align="left">Nationality</th>
</tr>
<tr>
    <td align="left">Mike</td>
    <td align="left">M</td>
    <td align="left">Singaporean</td>
</tr>

使用
DOMDocument::saveHTML

请参阅此示例代码:

$htmlString = '
    <td align="left">Mike</td>
    <td align="left">M</td>
    <td align="left">Singaporean</td>
';

$dom = new DOMDocument;
$dom->loadHTML($htmlString);
foreach($dom->getElementsByTagName('td') as $node)
{
    $arrayTd[] = $dom->saveHTML($node);
}

print_r($arrayTd);
$htmlString='1!'
迈克
M
新加坡人
';
$dom=新的DOMDocument;
$dom->loadHTML($htmlString);
foreach($dom->getElementsByTagName('td')作为$node)
{
$arrayTd[]=$dom->saveHTML($node);
}
打印(arrayTd);

您也可以使用任何csv阅读库。比如这个-。通过库进行操作将非常容易

您需要文件夹和位置作为参数来选择文件,您可以使用simpleExcel库来解析CSV,如下所示

function parseCSV($location,$folder){
    $excel = new SimpleExcel('CSV');

    try{
      $csv=$excel->parser->loadFile($location);
    }catch (Exception $ex){
      $logger->info($ex->getMessage());
    }

    $j = 1;
    /****column name ****/
    if($excel->parser->isRowExists($j)){
        $cols = $excel->parser->getRow($j);
    }

    $i=2;
    $arrval = array();
    /***csv data****/
    while($excel->parser->isRowExists($i)){
        $data=$excel->parser->getRow($i);
    }
}

使用此函数并传递所需的参数,然后执行var_dump($cols)和var_dump($data)。这没有经过测试,但会起作用

通读这篇文章可能会解决您的问题Yes@AnaadiAgrawal。这对我有用:)这是一个评论,不是答案
function parseCSV($location,$folder){
    $excel = new SimpleExcel('CSV');

    try{
      $csv=$excel->parser->loadFile($location);
    }catch (Exception $ex){
      $logger->info($ex->getMessage());
    }

    $j = 1;
    /****column name ****/
    if($excel->parser->isRowExists($j)){
        $cols = $excel->parser->getRow($j);
    }

    $i=2;
    $arrval = array();
    /***csv data****/
    while($excel->parser->isRowExists($i)){
        $data=$excel->parser->getRow($i);
    }
}