Horje
Learn PHP Extract Code

Tips (Total 2)


# Tips-1) How to get first Five Line from CSV File

A small change to your code should read a maximum of five lines.

How to extract first Five Line from CSV File

Follow the Example.
index.php
Example: PHP
<?php
$maxLines = 5;
$file_handle = fopen("file.csv", "r"); // Here put CSV File Location

for ($i = 0; $i < $maxLines && !feof($file_handle); $i++)
{
    $line_of_text = fgetcsv($file_handle, 1024);
    print '<div class="ticker_price">'. $line_of_text[0] . "</div>";
}
fclose($file_handle);
?>

Output should be:

How to extract first Five Line from CSV File

# Tips-2) How to get first 100 records from a csv file in PHP

Here is simple way to get first 100 records from csv file.

How to extract first 100 records from a TXT file in PHP

Follow the Example
index.php
Example: PHP
<?php
$start = 1; 
$end = 100;
$handle = fopen("file.csv", "r");
$i = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(($start <= $i) && ($i <= $end)) {

    
    echo $data[0];
    echo '</br>';
    
}
elseif($i > $end) {
    break;
}
$i++;
}

fclose($handle);
?>

Output should be:

How to extract first 100 records from a TXT file in PHP

Share on: