dtkyayvldeaqhl7151 2017-06-27 10:22
浏览 54
已采纳

PHP CSV导入,但没有前两行和一组信息

Hellos,

I would like to do a simple telephone list. The csv data I do get from a csv list from an active directory powershell export.

the csv is comma seperated and it's looking that way:

#TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser
name,"officephone"
firstnameA, lastnameA,"+49 12345 555 123"
firstnameB, lastnameB,"+49 12345 555 124"
firstnameC, lastnameC,"+49 12345 555 125"
firstnameD, lastnameD,"+49 12345 555 126"

So, and I would like to display it like that way (firstnameA, lastnameA in one row and the three digit number in the second row:

firstnameA, lastnameA              123
firstnameB, lastnameB              124
firstnameC, lastnameC              125
firstnameD, lastnameD              126

till yet I have that code:

<?php
echo "<html><body><table>

";
$phonebook = fopen("database/itlocal.csv", "r");


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

    echo "<tr class='departmenttext'>";
    foreach ($line as $cell) {
            echo "<td>" . htmlspecialchars($cell) . "</td>";
    }
    echo "</tr>
";
}
fclose($f);
echo "
</table></body></html>";
?>

How can I skip the first two lines and also cut the telephone number I'd like to have it?

thanks in advance

  • 写回答

1条回答 默认 最新

  • dongyulan6251 2017-06-27 10:26
    关注

    Try this:

    $lineCount = 0;
    while (($line = fgetcsv($f)) !== false) {
        if ($lineCount > 1) {
            echo "<tr class='departmenttext'>";
            foreach ($line as $key => $cell) {
                if ($key == 1) {
                   $cell = substr($cell, -3);
                   echo '';
                }
                echo "<td>" . htmlspecialchars($cell) . "</td>";
    
                if ($key == 1) {
                    echo "</tr>
    ";
                }
            }
            echo "</tr>
    ";
        }
        $lineCount = $lineCount + 1;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘