Jump to: navigation, search

PHP File Read Reverse

From w3cyberlearnings

Contents

PHP File Reverse Order

Read file from the last line to the first line.

File Content (fruits.txt)

Apple is red.
Apple is also green.
Apple is not blue.
Orange is yellow.
Orange is not blue.
Orange is red.

Example 1

<?php

$my_file = 'fruits.txt';
$lines = file($my_file);

for ($i = count($lines) - 1; $i >= 0; $i--) {
	echo $lines[$i] . '<br/>';
}
?>


Output

Orange is red. 
Orange is not blue. 
Orange is yellow. 
Apple is not blue. 
Apple is also green. 
Apple is red.

Example 2

  • Reverse word within each line.
<?php

$my_file = 'fruits.txt';
$lines = fopen($my_file, 'r') or exit('unable to read');

for ($i = 0; fseek($lines, $i, SEEK_END) !== -1; $i--) {
	echo fgetc($lines);
}
fclose($lines);
?>
 

Output

.der si egnarO .eulb ton si egnarO .wolley si egnarO .eulb ton si elppA .neerg osla si elppA .der si elppA

Related Links


  1. PHP File Linux vs. Windows
  2. PHP File Information
  3. PHP File Create
  4. PHP File Close
  5. PHP File Read
  6. PHP File Read Reverse
  1. PHP File Write
  2. PHP File Write to end or append to
  3. PHP File Write at Beginning
  4. PHP File Write at specific location
  5. PHP File Truncate
  6. PHP File Replace a specific word
  1. PHP File Replace a specific word with associative array
  2. PHP File Copy File
  3. PHP File Copy Reverse
  4. PHP File Search within file
  5. PHP File Delete File
  6. PHP File Template
  1. PHP File Email Template
  2. PHP File Template with MySQL
  3. PHP File Access with MySQL
Navigation
Web
SQL
MISC
References