PHP read TXT

February 21st, 2011 at 04:10pm Jason 53 views

Common Display:

<?php
$file = 'data.txt';  
$content = file_get_contents($file);  
echo $content; 
?>

The line you want to show:

<?php
$txt=file('data.txt');
echo $txt[3];        //It will show you line 4. because the beginning number is 0
?>

Get the Random:

<?php
$arr=file('data.txt');//txt name
$n=count($arr)-1;
for ($i=1;$i<=10;$i++){//Display 10 lines
  $x=rand(0,$n);
  echo $arr[$x],"\n";
}
?>

The Latest 20 lines:

<?php
$a = file("data.txt");
$b = count($a);
$c = $b-20;
for($i=$c;$i<$b;$i++)
{
  echo $a[$i]."\n";
}
?>
<?php
$txt=file('data.txt');
echo implode("\n", array_slice($txt,-20,20)); 
?>

Entry Filed under: Internet Tagged with:

Share This Post Share This Post SubscribePrint This Post Print This PostSave As PDF Save As PDF

Related Posts Hot Posts Hot Comments Hot Tags

      Leave a Comment

      Required

      Required, hidden

      Some HTML allowed:
      <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

      Trackback this post  |  Subscribe to the comments via RSS Feed


      Calendar

      February 2011
      MTWTFSS
      « Jan Mar »
       123456
      78910111213
      14151617181920
      21222324252627
      28 

      Sponsors

      Most Recent Posts