我正在尝试创建2个类,以封装这些文本文件,Customer.txt和Orders.txt。客户我需要将每行读入每行文本的单独数组实例,我该怎么做? 另外,我是否必须对文本文件中的逗号做些什么?
我知道我需要使用fopen,只是不确定如何去做。
这是我到目前为止所尝试过的。
<?php
class readOrders{
$array_of_lines = fopen("orders.txt", 'r') or die("Failed to create file");
foreach($array_of_lines as $line) {
$pieces_of_line = explode(',', $line);
}
class readCustomers{
$array_of_more_lines = fopen("customers.txt", 'r') or die("Failed to create file");
foreach($array_of_more_lines as $line) {
$pieces_of_line = explode(',', $line);
}
?>
这是我的文本文件
Orders.txt
1,4,0133360903,Building Java Programs,Computer Science
2,6,0321836995,Mathematics All Around,Mathematics
3,24,0321825721,Mathematics for Elementary Teachers with Activity Manual,Mathematics
4,10,0133011208,Business Math,Business
Customers.txt移动
2,Leonie,Köhler,leonekohler@surfeu.de,University of Stuttgart,Theodor-Heuss-Straße 34,Stuttgart,,Germany,70174,+49 0711 2842222
3,Bjørn,Hansen,bjorn.hansen@yahoo.no,University of Oslo,Ullevålsveien 14,Oslo,,Norway,0171,+47 22 44 22 22
4,François,Tremblay,ftremblay@gmail.com,McGill University,1498 rue Bélanger,Montréal,QC,Canada,H2G 1A7,+1 (514) 721-4711
答案 0 :(得分:0)
我认为 fgetcsv 在这里很有用,例如来自php-doc:
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>