我的文本文件sample.txt。我想从文本文件中排除第一行,并将其他行存储到mysql数据库中。
ID Name EMail
1 Siva xyz@gmail.com
2 vinoth xxx@gmail.com
3 ashwin yyy@gmail.com
现在我想从文本文件中读取除第一行(ID,名称,电子邮件)以外的数据并将其存储到MYsql数据库中。因为我已经在数据库中创建了一个具有相同名称的字段。
I have tried
$handle = @fopen($filename, "r"); //read line one by one
while (!feof($handle)) // Loop till end of file.
{
$buffer = fgets($handle, 4096); // Read a line.
}
print_r($buffer); // It shows all the text.
请告诉我怎么做?
感谢。 问候, Siva R
答案 0 :(得分:2)
使用file()会更容易,因为它会获取数组中的所有行:
// The rows still contain the line break, since we only trimmed the copy
$content = implode("\n", $rows);
如果你想再次将它全部作为一个字符串,没有第一行,只需用一个新行作为胶水内容:
file()
注意: 正如@ Don&#t; tPanic在评论中指出的那样,使用{{1}}简单易行,但不是建议如果原始文件很大,因为它会将整个内容作为数组读入内存(并且数组占用的内存多于字符串)。他还正确地推荐了FILE_IGNORE_NEW_LINES-flag,因此你知道: - )
答案 1 :(得分:1)
您可以在while循环之前调用fgets
一次,以便将标题行排除在外。
$firstline = fgets($handle, 4096);
while (!feof($handle)) // Loop till end of file.
{ ...