
在本文中,我们将学习如何使用PHP的fgetcsv()、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.
让我们通过下面的例子来理解这个问题 -
在这个例子中,我们将使用fgetcsv()函数读取一个数据CSV文件,并使用HTML表格标签以表格形式显示这些值。
立即学习“PHP免费学习笔记(深入)”;
这个例子中使用的CSV文件−
Name, Age, City John, 30, New York Mary, 25, Los Angeles Tom, 40, Chicago
<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>
在这个示例中,我们将读取一个包含学生姓名、年龄和性别的学生CSV文件,并使用3种不同的方法(使用str_getcsv、fgetcsv和SplFileObject方法)以表格形式显示他们的数据。
Name,Age,Gender John Doe,25,Male Jane Smith,30,Female Bob Johnson,40,Male Sara Lee,22,Female
<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, "</p><p>"); // 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>';
?>
总之,使用PHP显示来自CSV文件的数据是一个简单的过程,可以使用fgetcsv()函数。借助HTML标签,我们可以轻松地以表格形式显示数据。通过遵循本文提供的示例,我们学会了在PHP中读取和显示来自CSV文件的数据,这在各种应用中都很有用。
以上就是使用PHP从CSV文件中提取和显示数据的方法的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号