Jump to content

Single or several lines from txt file


Mindaugas

Recommended Posts

And assuming the file will always fit into memory, try this simple solution -

 

<?php
$the_file = 'text.dat'; // what file to read
$lines = file($the_file); // read the file into an array

// example 1
$find_these = array(1); // the line(s) you want
foreach($find_these as $key){
$index = $key -1; // adjust to match actual index values (start at zero)
echo "Line: $key, $lines[$index]<br />";
}

// example 2
$find_these = array(1,2,3); // the line(s) you want
foreach($find_these as $key){
$index = $key -1; // adjust to match actual index values (start at zero)
echo "Line: $key, $lines[$index]<br />";
}
?>

Link to comment
Share on other sites

And assuming the file will always fit into memory, try this simple solution -

 

<?php
$the_file = 'text.dat'; // what file to read
$lines = file($the_file); // read the file into an array

// example 1
$find_these = array(1); // the line(s) you want
foreach($find_these as $key){
$index = $key -1; // adjust to match actual index values (start at zero)
echo "Line: $key, $lines[$index]<br />";
}

// example 2
$find_these = array(1,2,3); // the line(s) you want
foreach($find_these as $key){
$index = $key -1; // adjust to match actual index values (start at zero)
echo "Line: $key, $lines[$index]<br />";
}
?>

 

Thx it`s working perfectly :)

why txt file is .dat not .txt ?

Link to comment
Share on other sites

I see. Well personally I keep any files that I don't want people to have access to outside of the document root folder, so that they aren't accessible via any uri other than internally on the server. I suppose you could use a .htaccess file to block certain file extensions from being access too

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.