微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用PHP从CSV文件中提取和显示数据的方法

使用PHP从CSV文件中提取和显示数据的方法

在本文中,我们将学习如何使用PHPfgetcsv()、str_getcsv和SplFileObject函数从CSV文件显示数据。

CSV file is a simple file format used to store data with comma-separated values, and each row in it represents a record in the tabular data. To read a CSV file using PHP, we will use the fgetcsv() function which reads a line from a CSV file and returns an array of values representing the CSV data present in that line.

让我们通过下面的例子来理解这个问题 -

Example 1

中文翻译为:

示例1

在这个例子中,我们将使用fgetcsv()函数读取一个数据CSV文件,并使用HTML表格标签以表格形式显示这些值。

这个例子中使用的CSV文件

Data.csv

Name, Age, City
John, 30, New York
Mary, 25, Los Angeles
Tom, 40, Chicago

文件名:index.PHP

<html lang="en">
<head>
   <title>How to display Data from CSV file using PHP?</title>
</head>
<body>
  <h3>How to display Data from CSV file using PHP?</h3>
  <?PHP
    $csvFile = fopen('Data.csv', 'r');

    echo '<table>';
    while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) {
      echo '<tr>';
      foreach ($data as $value) {
        echo '<td>' . htmlspecialchars($value) . '</td>';
      }
      echo '</tr>';
    }
    echo '</table>';

    fclose($csvFile);
	?>
</body>
</html>

Example 2

中文翻译为:

示例2

在这个示例中,我们将读取一个包含学生姓名、年龄和性别的学生CSV文件,并使用3种不同的方法(使用str_getcsv、fgetcsv和SplFileObject方法)以表格形式显示他们的数据。

Students.csv

中文翻译为:

Students.csv

Name,Age,Gender
John Doe,25,Male
Jane Smith,30,Female
Bob Johnson,40,Male
Sara Lee,22,Female

文件名:index.PHP

<html lang="en">
<head>
   <title>How to display Data from CSV file using PHP?</title>
</head>
<body>
  <h3>How to display Data from CSV file using PHP?</h3>
  
  <!-- Method 1: str_getcsv -->
  <?PHP
    $csvData = file_get_contents('students.csv');
    $rows = str_getcsv($csvData, "

"); // Split CSV data into rows echo '<h4>Method 1: str_getcsv</h4>'; echo '<table>'; echo '<thead><tr><th>Name</th><th>Age</th><th>Gender</th></tr></thead>'; echo '<tbody>'; foreach ($rows as $row) { $values = str_getcsv($row, ","); // Split row into values echo '<tr>'; foreach ($values as $value) { echo '<td>' . htmlspecialchars($value) . '</td>'; } echo '</tr>'; } echo '</tbody></table>'; ?> <!-- Method 2: Combine fgetcsv() and HTML --> <?PHP echo '<h4>Method 2: Combine fgetcsv() and HTML</h4>'; $csvFile = fopen('students.csv', 'r'); echo '<table>'; echo '<thead><tr><th>Name</th><th>Age</th><th>Gender</th></tr></thead>'; echo '<tbody>'; while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) { echo '<tr>'; foreach ($data as $value) { echo '<td>' . htmlspecialchars($value) . '</td>'; } echo '</tr>'; } echo '</tbody></table>'; fclose($csvFile); ?> <!-- Method 3: using SplFileObject --> <?PHP echo '<h4>Method 3: using SplFileObject</h4>'; $csvFile = new SplFileObject('students.csv', 'r'); $csvFile->setFlags(SplFileObject::READ_CSV); echo '<table>'; echo '<thead><tr><th>Name</th><th>Age</th><th>Gender</th></tr></thead>'; echo '<tbody>'; foreach ($csvFile as $row) { echo '<tr>'; foreach ($row as $value) { echo '<td>' . htmlspecialchars($value) . '</td>'; } echo '</tr>'; } echo '</tbody></table>'; ?>

Conclusion

总之,使用PHP显示来自CSV文件的数据是一个简单的过程,可以使用fgetcsv()函数。借助HTML标签,我们可以轻松地以表格形式显示数据。通过遵循本文提供的示例,我们学会了在PHP中读取和显示来自CSV文件的数据,这在各种应用中都很有用。

以上就是使用PHP从CSV文件提取显示数据的方法的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐