0

I am grabbing a .txt file and trying to reverse it, but I get this error when I try to, I don't understand it. Help please?

array_reverse() expects parameter 1 to be array, string given in ......

Here is the code:

$dirCont = file_get_contents($dir, NULL, NULL, $sPoint, 10240000);
$invertedLines = array_reverse($dirCont);

echo $invertedLines;
alex
  • 79
  • 1
  • 3
  • 4

2 Answers2

2

A string is not an array? Even if it were (as in C strings) it would not work as you expected. You'll need to split the file on line breaks (if you're trying to reverse to get the end of the file first).

$invertedLines = array_reverse(preg_split("/\n/", $dirCont));
Suroot
  • 4,315
  • 1
  • 22
  • 28
  • For clarification, what Suroot is trying to say is that the file_get_contents function you're using returns a string, not an array, so you're feeding array_reverse a string, which of course isn't going to work, http://us3.php.net/file_get_contents –  Jun 15 '11 at 03:35
  • Thank you Suroot, however I am recieving another error now, and the same array_reverse error aswell: Warning: preg_split() [function.preg-split]: Unknown modifier 'v' in – alex Jun 15 '11 at 03:40
  • Can you paste the new code that you're using? Seems like you're trying to pass preg_split some flags that it's not happy with. Also, post all of the errors that you are seeing. Probably be best to edit the original post with the new information. – Suroot Jun 15 '11 at 03:44
  • $dirCont = file_get_contents($dir, NULL, NULL, $sPoint, 10240000); $invertedLines = array_reverse(preg_split($dirCont, "\n")); Two errors are: Warning: preg_split() [function.preg-split]: Unknown modifier 'v' in and Warning: array_reverse() expects parameter 1 to be array, boolean given – alex Jun 15 '11 at 03:47
  • Ha, my fault; not having a good day today; need to reverse the parameters in preg_split(). Will update, sorry for the confusion. Also need to include // around the pattern, hence /\n/ – Suroot Jun 15 '11 at 03:51
  • Nevermind, I made a function to print everything in the Array, it works like a charm! Thanks so much for your help! – alex Jun 15 '11 at 04:37
1

I think you need to pass the value on an array.

array_reverse(array($dircont));

This is working fine for me.