need help with this small code!

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
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

need help with this small code!

Post by nicosns »

Hello,

I'm still a newbie when it comes to PHP, so I need help to correct this code.

What does it do?
It opens a file (file.txt), then it looks if there are duplicates en when they do the code will hide one or more of the double ones, and final it shows the code without the dups + ONLY the last 10 from the list. But the code is not finished, so I get errors. It must be something wth the 'echo implode' thing.

Code: Select all

<?php
$file = "file.txt";
$dups = array_unique (explode ("\n", str_replace("\n<a href=\x22http://www.domain.com/start/\x22></a></br>", "", file_get_contents($file))));
echo implode ("\n", $dups)("",array_slice(file("$file"),-10,10));
?>
I hope someone can tell me what I'm doing wrong. Thanks!
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: need help with this small code!

Post by hansford »

the code's do jumbled it's hard to tell what you're trying to do. Why not break some of those functions down so it's more readable. Also, this 'code' opens a file and looks for duplicates of what? Then it's suppose to do exactly what?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: need help with this small code!

Post by superdezign »

nicosns wrote:

Code: Select all

echo implode ("\n", $dups)("",array_slice(file("$file"),-10,10));
Syntax error.

Code: Select all

echo implode ("\n", $dups)

Code: Select all

("",array_slice(file("$file"),-10,10));
The second part seems to be a stray.
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

Re: need help with this small code!

Post by nicosns »

I tried to combine 2 codes...

file.txt contains something like this:
marc
peter
ellen
martin
petra
...
------------------------------------------

Code 1: includes 'file.txt' en shows only the last 10 lines from that text file.

Code: Select all

<?php
 
$file = "file.txt";
echo implode("",array_slice(file("$file"),-10,10));
 
?>
-------------------------------

code2: includes 'file.txt', looks for duplicates + replaces something, and then it show the WHOLE list (without dups).

Code: Select all

<?php
 
$file = "file.txt";
$dups = array_unique (explode ("\n", str_replace("\n |  ", "", file_get_contents($file))));
echo implode ("\n", $dups);
 
?>
************************
Now I want to combine this 2 codes so I'll end with:
inlcudes 'file.txt', looks for duplicates + replaces something, and then it shows only the 10 last lines (now without dups). But I have a problem with the 2 'echo implodes'. How do you combine 2 echo implodes from 2 codes?

Code: Select all

<?php
$file = "file.txt";
$dups = array_unique (explode ("\n", str_replace("\n<a href=\x22http://www.domain.com/start/\x22></a></br>", "", file_get_contents($file))));
echo implode ("\n", $dups)("",array_slice(file("$file"),-10,10));
?>
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

Re: need help with this small code!

Post by nicosns »

Anyone can help with this small code, please?
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: need help with this small code!

Post by WebbieDave »

I'm not sure why you need to do the following:

Code: Select all

str_replace("\n<a href=\x22http://www.domain.com/start/\x22></a></br>", "", file_get_contents($file));

This will result in a parse error:

Code: Select all

echo implode ("\n", $dups)("",array_slice(file("$file"),-10,10));

To get the last ten elements of an array, try this:

Code: Select all

$dupsCount = count($dups);  // should really be called nodups
if ($dupsCount > 10) {
    $offset = $dupsCount - 10;
} else {
    $offset = 0;
}
$lastTen = array_slice($dups, $offset);
Post Reply