Hello I just read a document using PHP and I need to skip the first 58 lines of that document and then start sending the data that follows from those lines to my database. How do I go about skipping the first 58 lines of the document.

I really need help thank you in advance.

Dani AI

Generated

Both of the suggestions in this thread are valid: 's quick in‑memory split works for small files, and 's streaming approach avoids loading the whole file. For a reliable import into a database in production, prefer a streaming pattern (low memory) plus safe, batched DB writes. Below are two practical, memory‑efficient patterns you can drop into an import workflow and a few operational tips.

$fp = fopen('sometextfile.txt', 'r');
$skip = 58;

// skip first $skip lines
for ($i = 0; $i < $skip && !feof($fp); $i++) {
    fgets($fp);
}

$pdo->beginTransaction();
$stmt = $pdo->prepare('INSERT INTO mytable (col1,col2) VALUES (?,?)');
$counter = 0;
$batchSize = 1000;

while (($line = fgets($fp)) !== false) {
    $line = trim($line);
    if ($line === '') continue;

    // parse the line (use fgetcsv/str_getcsv if CSV)
    list($col1, $col2) = explode("\t", $line, 2);

    $stmt->execute([$col1, $col2]);
    if (++$counter % $batchSize === 0) {
        $pdo->commit();
        $pdo->beginTransaction();
    }
}
$pdo->commit();
fclose($fp);

Or use a small generator for the same lazy behavior, which makes the loop code cleaner:

function linesAfter($file, $skip) {
    $fp = fopen($file, 'r');
    $i = 0;
    while (($line = fgets($fp)) !== false) {
        if ($i++ < $skip) continue;
        yield $line;
    }
    fclose($fp);
}

foreach (linesAfter('sometextfile.txt', 58) as $line) {
    // process line
}

Notes and cautions: use prepared statements and transactions for performance and safety; commit in batches for very large imports; use fgetcsv or str_getcsv for true CSV input; set set_time_limit(0) if imports run long; close resources and check $_FILES['file']['tmp_name'] when handling uploads. If the header is variable instead of a fixed 58 lines, read until a known marker instead of counting lines. These patterns avoid the memory pitfalls and give predictable, fast imports.

Recommended Answers

All 5 Replies

Maybe you are looking for:

$str = implode("\n", array_slice(explode("\n", $str), 58));

while $str is your document content and 58 the lines to be skipped

-Agarsia

Agarsia thanks I'll try it out.

thanks Argasia it works really appreciate it.

Be careful when using code like that with large files. That relies on your entire file being first read into memory.

The better way to do this would be to iterate over the file line by line.

<?php
$it = new LimitIterator( new SplFileObject( 'sometextfile.txt' ), 57 );

foreach( $it as $line ){
  echo $line;
}

http://www.php.net/manual/en/class.splfileobject.php
http://www.php.net/manual/en/class.limititerator.php

Okay, mschroeder your help is really appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.