您好,欢迎访问一九零五行业门户网

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

在本文中,我们将学习如何使用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.
让我们通过下面的例子来理解这个问题 -
example 1的中文翻译为:示例1在这个例子中,我们将使用fgetcsv()函数读取一个数据csv文件,并使用html表格标签以表格形式显示这些值。
这个例子中使用的csv文件−
data.csvname, age, cityjohn, 30, new yorkmary, 25, los angelestom, 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.csvname,age,genderjohn doe,25,malejane smith,30,femalebob johnson,40,malesara 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文件中提取和显示数据的方法的详细内容。
其它类似信息

推荐信息