I am currently reading and displaying the entire contents of a text file using the following code:

$html_get = file('content.txt');
foreach ($html_get as $html_num => $html_text) {
echo $html_text;
}

I would like, however, to have the code read the contents of the text file up to a certain line of code eg <!-- StopReadingHere -->. And a similar code to start reading from a line of code in a similar way.

Anyone know how best to do this?

Thanks,
Martin

Dani AI

Generated

martinkorner wanted to stop reading a text file at a marker like <!-- StopReadingHere -->. suggested a line-by-line loop and suggested explode. The reported symptom — the marker not being found even when present on its own line — is usually caused by hidden characters: trailing CR/LF, extra spaces, or a UTF-8 BOM. Exact equality against a raw fgets() buffer will fail when those characters are present.

A simple, robust approach is to read the whole file, strip any leading BOM, find the first occurrence of the marker and take the substring up to (or after) it. This works even if the marker appears mid-line:

<?php
$marker = '<!-- StopReadingHere -->';
$txt = file_get_contents('content.txt');
// remove UTF-8 BOM if present
if (substr($txt, 0, 3) === "\xEF\xBB\xBF") $txt = substr($txt, 3);
$pos = strpos($txt, $marker);
$left = ($pos === false) ? $txt : substr($txt, 0, $pos);
echo $left;

For very large files, prefer streaming: read line-by-line, normalize each line with rtrim($line, "\r\n") or trim() and then check strpos($line, $marker) !== false (or use trim($line) === $marker when the marker must occupy the whole line). That fixes the common whitespace/newline mismatch that saw. Alternatively, 's explode($marker, $txt, 2) yields the left/right parts after a single read.

Notes and cautions: remove BOMs when present, account for Windows \r\n vs Unix \n, choose stripos/mb_stripos for case-insensitive or multibyte-safe searches, and prefer streaming when memory use matters.

Recommended Answers

All 4 Replies

Hi martinkorner,

Try using strpos along with fread. Aside from that, you could read in a line of the file at a time:

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
$token = '<!-- STOP HERE -->';
if ($handle) {
    $superbuffer = array();
    while (!feof($handle)) {
        $buffer = fgets($handle);
        if($buffer == $token) {
          echo 'Stopping...';
          // Jump out of this loop
        } else {
          $superbuffer[] = $buffer;
        }
    }
    fclose($handle);
    echo join('<br>',$superbuffer);
}
?>

Of course, I didn't try the code above, but in theory, that would do what you want it too. You can also specify a length for fgets to read so that it only reads the first 39 characters or whatever.

I hope this is somewhat close to what you are looking for.

Great question by the way!

Thanks chrelad,

I tried this, and tried reading each line individually, but the 'stop' term oddly never gets found (I have checked I'm searching for the correct term - which is on it's own line).
I wonder if this is because the $buffer holds a space after the line from the text file. I have tried searching for the 'stop' term with a space after it, but still no luck.

Any ideas?

Speak Soon,
Martin

Hi martinkorner,

You may have better luck using regular expressions to find the search term instead of using '==' syntax. There are flags which tell preg_match/ereg_match to ignore whitespaces and the like... You could try that and see what you come up with. That's where I'd start anyway :)

Hi martinkorner,

You may have better luck using regular expressions to find the search term instead of using '==' syntax. There are flags which tell preg_match/ereg_match to ignore whitespaces and the like... You could try that and see what you come up with. That's where I'd start anyway :)

or try with explode

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.