Why's my reverse_array() not working?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Why's my reverse_array() not working?

Post by Dale »

Im using this:

Code: Select all

*snip*
$statsї'url'] = "$mybb_forumurl";
$lines = @file("".$statsї'url']."hum.php"); 

if(!$lines) {
if(!$statsї'posts'] || !$statsї'threads'] || !$statsї'members']) {
echo "<td colspan="3" bgcolor="#FFFFFF" nowrap><font face="Tahoma" size="1" color="#FF0000"><b>Cannot Find Stats For:<br>$stats&#1111;url]<br><a href="".$stats&#1111;'url']."stats.php" target="_blank">View External Stats.</a></b></font></td>";
&#125; else &#123;
echo "<td width="4%" bgcolor="#FFFFFF" nowrap><font face="Tahoma" size="1">".$stats&#1111;'posts']."</td>";
echo "<td width="4%" bgcolor="#FFFFFF" nowrap><font face="Tahoma" size="1">".$stats&#1111;'threads']."</td>";
echo "<td width="4%" bgcolor="#FFFFFF" nowrap><font face="Tahoma" size="1">".$stats&#1111;'members']."</td>";
&#125;
&#125; else &#123;
foreach ($lines as $line_num => $line) &#123; 
$input  = array(htmlspecialchars($line));
$arr&#1111;] = array_reverse($input);
&#125;
$html = implode('', $lines);
echo "<td width="4%" bgcolor="#FFFFFF" nowrap><font face="Tahoma" size="1">$arr&#1111;4]</font></td>";
echo "<td width="4%" bgcolor="#FFFFFF" nowrap><font face="Tahoma" size="1">$arr&#1111;3]</font></td>";
echo "<td width="4%" bgcolor="#FFFFFF" nowrap><font face="Tahoma" size="1">$arr&#1111;2]</font></td>";
&#125;
*snip*
But for some reason its not showing the 4th to last, 3rd to last or the 2nd to last line of the document hum.php. Its just showing a blank space... why?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

first off, array[4] is the 5th element. Your code is calling reverse on a single element array as htmlspecialchars returns a string, which is placed as the sole element in an array, which is reversed and then added to an array.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

feyd wrote:first off, array[4] is the 5th element. Your code is calling reverse on a single element array as htmlspecialchars returns a string, which is placed as the sole element in an array, which is reversed and then added to an array.

8O What?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

feyd's notes:
  • array[4] is the 5th element in an array
  • htmlspecialchars returns a string.
  • calling reverse on a single element array is.... useless
  • the actual array is never reversed.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

*me = n00b*

So then how do i reverse it then? Because everyones global.php file is different lengths so its usless me trying to return line 101 from the script... so i want to go upwards instead of down.

*me = n00b*
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

untested example

Code: Select all

$data = @file_get_contents($stats&#1111;'url'] . 'hum.php');
if(empty($data))
&#123;
// do your empty junk
&#125;
else
&#123;
  $lines = array_slice(preg_split("#\r\n|\n\r|\r|\n#", htmlentities($data, ENT_QUOTES)), -4, 0, true);
  var_export($lines);
&#125;
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

are


^ What are they supposed to be?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't understand what you are asking.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

feyd wrote:I don't understand what you are asking.

In that sample code you posted the word 'are' is in blue... is that what it supposed to be? Or is this supposed to be something else?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read the suggestion board for why they appear.. thank your fellow members for it. :|

if you look at the actual post, they aren't there... as you'll find out if you just quote my post or read the explaination of where the r comes from.:roll:
Post Reply