How to delete two first lines in txt using PHP

Boby

New Member
#1
For example I have data:
Mã:
File generated by system automatically...
9 rows selected.

Heber,Camrynborough,26728,Home Health Aide
Modesto,West Janet,15152-2683,Software Engineer
Dante,East Chanel,74689-6886,Entertainment Attendant
Nolan,Murphyville,32561-8079,Credit Authorizer
Jovany,O'Reillyton,44371,Medical Assistant
Jaeden,Greenfort,06179-1759,School Social Worker
Efrain,West Blairborough,11282-0496,Electronic Drafter
Travon,South Tatum,76603-0822,Manufactured Building Installer
Agustina,North Gertrudeland,18950,Health Services Manager
and simple php code to import data into mysql
Mã:
$open = fopen('employee-data.txt','r');

while (!feof($open))
{
    $getTextLine = fgets($open);
    $explodeLine = explode(",",$getTextLine);

    list($name,$city,$postcode,$job_title) = $explodeLine;

    $qry = "insert into empoyee_details (name, city,postcode,job_title) values('".$name."','".$city."','".$postcode."','".$job_title."')";
    mysqli_query($conn,$qry);
}

fclose($open);
It works fine, but problem is on start read file, because file contains three useless lines (two with text and last one is empty). How can I delete lines before start import data into mysql?
 

Admin

Administrator
Thành viên BQT
#2
After opening the file, call fgets() 3 times to skip over those lines.
Mã:
$open = fopen('employee-data.txt','r');
# skip heading lines.
fgets($open);
fgets($open);
fgets($open);
Also, you shouldn't use while (!feof($open)).

Use:
Mã:
while ($explodedLine = fgetcsv($open)) {
    ...
}
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top