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
- PHP File Linux vs. Windows
- PHP File Information
- PHP File Create
- PHP File Close
- PHP File Read
- PHP File Read Reverse